
코드 입력
test_case = int(input())
cnt = 0
for _ in range(1, test_case+1):
test_ = input()
val_word = []
before_alphabet = ''
while True:
if len(test_) >= 2:
if test_[0] not in val_word:
val_word.append(test_[0])
before_alphabet = test_[0]
test_ = test_[1:]
elif test_[0] not in val_word:
val_word.append(test_[0])
before_alphabet = test_[0]
test_ = test_[1:]
elif test_[0] in val_word and before_alphabet == test_[0]:
test_ = test_[1:]
elif test_[0] in val_word and before_alphabet != test_[0]:
break
elif len(test_) == 1:
if val_word == [] and before_alphabet == '':
cnt += 1
break
elif test_ not in val_word:
cnt += 1
break
elif test_ in val_word and before_alphabet == test_:
cnt += 1
break
elif test_ in val_word and before_alphabet != test_:
break
예외 처리하는 문제의 싸움이었다.
내가 푼 로직보다 더 깔끔하고 간단하게 풀 수 있을지도 모르겠다.
다만, 내가 생각했을 땐 가장 중요한건
-
이전에 등장한 알파벳이 다시 등장하면 안되고,
-
등장하더라도 바로 이전 알파벳과 같다면 괜찮다였다.
등장한 알파벳의 경우, 리스트에 추가하고 넘어가기. 만약 이미 리스트에 있고 이전 알파벳과 같다면 넘어가고,
아니라면 break로 탈출.
글자 수가 0이 될 때까지 단순 break 조건에 걸리지 않는다면 cnt에 1 추가한 후 break하는 로직이다.
if문과의 싸움이었는데, 더 쉽게 풀 수 있는 로직이 있다면 좋을 것 같다.
개인적으로 코드가 깔끔한게 더 좋아서..