Skip to content

Commit 4d223f1

Browse files
Address @dg-pb's review re. reverse string search complexity
Co-authored-by: dgpb <3577712+dg-pb@users.noreply.github.com>
1 parent 29e2017 commit 4d223f1

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

Doc/library/time-complexity.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ the buffer instead of moving the remaining bytes, and is amortized *O*\ (1).
204204
- *O*\ (len(*s*) + len(*t*))
205205
* - Multiply (``s * k``)
206206
- *O*\ (*nk*)
207-
* - Substring search (``x in s``, ``s.find(x)``, ``s.index(x)``) [10]_
207+
* - Substring search (``x in s``, ``s.find(x)``, ``s.index(x)``)
208208
- *O*\ (*n*)
209+
* - Reverse substring search (``s.rfind(x)``, ``s.rindex(x)``) [10]_
210+
- *O*\ (*nk*)
209211
* - Encode or decode
210212
- *O*\ (*n*)
211213
* - Iteration
@@ -306,11 +308,12 @@ Notes
306308
See the :ref:`note on concatenating immutable sequences
307309
<typesseq-repeated-concatenation>` for alternatives.
308310
309-
.. [10] A naive substring search would need *O*\ (*nk*) comparisons in the
310-
worst case, where *k* is the length of the substring searched for, but
311-
CPython uses search algorithms with a linear worst case for forward
312-
searches. See :source:`Objects/stringlib/stringlib_find_two_way_notes.txt`
313-
for details.
311+
.. [10] *k* is the length of the substring searched for. Forward searches use
312+
algorithms with a linear worst case, described in
313+
:source:`Objects/stringlib/stringlib_find_two_way_notes.txt`. Reverse
314+
searches use a simpler algorithm, which is *O*\ (*n*) on typical input but
315+
has no linear worst case. ``s.rpartition(x)`` and ``s.rsplit(x)`` search
316+
backwards too.
314317
315318
.. [11] Assuming :class:`int` or :class:`bool` arguments. For other types,
316319
the range is searched like any other sequence in *O*\ (*n*) time.

0 commit comments

Comments
 (0)