擅长:python、mysql、java
<p>你的答案很简单。你已经解决了一半的问题。在字符串上迭代3次。您需要知道的是,测试这些子字符串中是否有任何一个子字符串等于字符串<code>"bob"</code>。如果是,则将<code>bob</code>计数器增加<code>1</code>:</p>
<pre><code>>>> s = 'azcbobobegghakl'
>>> bob = 0
>>>
>>> for i in range(len(s)):
substr = s[i: i + 3]
# is the current three letter substring equal to "bob"?
if substr == 'bob':
# if so, increment the counter by one.
bob += 1
>>> bob
2
>>>
</code></pre>