1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python3 文字列の重複を調べる方法

Posted at

#①countを使用する場合

print("Good School".count("oo"))
>> 2

####注意
countは数えた文字は再度数えない

print("Goooood".count("oo"))
>> 2

#②全重複を考える場合
上記の内容を結果3としたいのような場合

pattern = input()
string = input()
result = 0

for i in range(len(string) - len(pattern) + 1):
    portion = string[i : i + len(pattern)]

    if portion == pattern:
        result += 1

print(result)

入力例1
AA
abdeeAAbAAAbfde

出力例1
3

###まとめ
上記のように重なるものをどのように扱うかで異なる結果になる。注意して導入しよう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?