LoginSignup
0
1

More than 3 years have passed since last update.

【Python】ABC - 098 - Cut and Count

Posted at

問題

与えられた英小文字で構成されている文字列を2つに分割して、それぞれに共通して含まれる英小文字の種類が最大となる分割を探して、
共通する英小文字の数を出力する問題。

方針

文字列の左から順番に分割するパターンを試していって、共通する英小文字の数がその時点での最大値となったら更新。

工夫

  1. 漏れのないようにスライスで部分文字列に分割
  2. それぞれの部分文字列をset型に変換して、共通する要素を求めるために積集合をとる
N = int(input())
S = input()
# 共通する英小文字の種類の初期化
cnt = 0
for i in range(1, N):
    # スライスで部分文字列をつくって、set型に変換し、積集合で共通する英小文字を取得
    dup_cnt = set(S[:i]) & set(S[i:])
    # 共通する英小文字の数が現時点での最大値より大きいなら最大値を更新
    if cnt < len(dup_cnt):
        cnt = len(dup_cnt)
print(cnt)
0
1
2

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
0
1