I was trying some question task was to find the number of times a substring appears in a given string. Given string was "ABCDCDC" and substring "CDC" I used the .count() method for string with gives me answer 1, which is wrong. So Please let me know the reason.
Asked
Active
Viewed 42 times
0
-
1Can you share the code you've tried? – Mureinik Apr 05 '20 at 11:46
-
https://stackoverflow.com/a/37980892/11701071 check this out – muyustan Apr 05 '20 at 11:50
-
1Does this answer your question? [String count with overlapping occurrences](https://stackoverflow.com/questions/2970520/string-count-with-overlapping-occurrences) – Adam.Er8 Apr 05 '20 at 11:52
2 Answers
0
But if the example is "ABCDCDC" , then count is right... if you search for "CDC" explicitly.
"AB cdc DC" so CDC is just once in a string.

TheOriginalOdis
- 82
- 2
- 8
0
The docs for str.count
explicitly mentions this behavior:
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
See this answer for counting substrings with overlaps

Adam.Er8
- 12,675
- 3
- 26
- 38