import sys

num = int(input())
n=0

for i in range(num):
    wl=[]
    isSequence = 2
    isNow = False
    word = list(sys.stdin.readline())
    word.remove('\n')
    word_set = list(set(word))
    wl.append(word[0])
    
    if len(word) == len(word_set): # 중복되는 알파벳이 존재 x
        n+=1
    elif len(word_set) == 1:
        n+=1
    else: # 중복되는 알파벳이 존재 o -> 모두 연속되는가를 확인
        for k in range(len(word)-1):
            if isSequence == 2 or isSequence == 1:
                if word[k] == word[k+1]:
                    wl.append(word[k])
                    wl = list(set(wl))
                    isNow = True
                    isSequence = 1
                else:
                    wl.append(word[k+1])
                    if isNow:
                        wl_set = list(set(wl))
                        if len(wl) != len(wl_set): # 연속이 아니다!
                            isSequence = 0
                        else:
                            isSequence = 1
            else:
                break
        if isSequence == 1:
            n+=1

print(n)

 

처음 풀 때 엄청 복잡하게 접근했었다.

그 결과 끊임없이 반례들이 발생했고 처음부터 다시 풀고자 했다.

 

list의 슬라이싱n을 1씩 빼는 방법(보통의 풀이와는 반대된 접근)으로 훨씬 간단하게 해결할 수 있었다.

n = int(input())

for i in range(n):
    word = list(input())
    
    for k in range(len(word)-1):
        if word[k] == word[k+1]: 
            pass
        elif word[k] in word[k+2:]:
            n-=1
            break
            
print(n)

 

 

 

문제: https://www.acmicpc.net/problem/1316

 

1316번: 그룹 단어 체커

그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때

www.acmicpc.net

'Baekjoon > python' 카테고리의 다른 글

[백준 2292번] 벌집  (0) 2023.02.18
[백준 1712번] 손익분기점  (0) 2023.02.18
[백준 2941번] 크로아티아 알파벳  (0) 2023.02.17
[백준 5622번] 다이얼  (0) 2023.02.08
[백준 2908번] 상수  (0) 2023.02.03

+ Recent posts