aisiunme.github.io/algorithm/2018/08/13/baekjoon-group-word-checker-1316.md/
백준 온라인 저지 - 그룹 단어 체커(1316)
Baekjoon Problem #1316 - 조건에 맞는 단어를 찾는 문제
aisiunme.github.io
Jun Lee 님의 글을 참고하였습니다.
우선 제 코드는 틀렸습니다.
cnt=0
for _ in range(int(input())):
w={}
idx=""
word=input()
if len(word)==word.count(word[0]) and len(word)>2:
continue
for i in word:
if (i not in w)or idx==i:
w[i]=True
else:
w[i]=False
idx=i
if False not in list(w.values()):
cnt += 1
print(cnt)
- 딕셔너리로 풀어보려 했는데 쓸데없는 잡생각에 이것 저것 붙이다보니 코드도 이상해지고 답도 안 나왔다.
- 접근법을 찾아보니
result = int(input())
for _ in range(result):
word = input()
for i in range(1, len(word)):
if word.find(word[i-1]) > word.find(word[i]):
result -= 1
break
print(result)
- 이런 식으로 깔끔하게 해결했다.
- 이 코드를 해석하자면
if word.find(word[i-1]) > word.find(word[i]):
인덱스를 비교했을 때
뒤에 있는 글자 -> word.find(word[i])의 인덱스가
i-1의 인덱스보다 작다면, 즉 글자는 뒤에 있지만 인덱스가 이전 글자보다 작다면
재등장한 단어(그룹단어X)가 된다.
'알고리즘 > 백준' 카테고리의 다른 글
백준 Python - 14698 전생했더니 슬라임 연구자였던 건에 대하여 (Hard) (0) | 2021.02.15 |
---|---|
백준 Python - 2941 크로아티아 알파벳 (0) | 2021.02.12 |
순열과 조합 정복하기! 백준 15649~15666번 (0) | 2021.02.10 |
백준 Python - 1931 회의실 배정 (0) | 2021.02.10 |
백준 Python - 11399 ATM (0) | 2021.02.08 |