본문 바로가기

코딩 개발자의 하루/Python Programming

[백준 1316번] 그룹단어체커

이번 문제는 내게 난이도가 있었다.

 

우선, 아래처럼 접근을 잘못했다.

cal() 함수를 만들어 x[i]가 store 리스트에 속해있다면 result을 0으로 내뱉는 함수이다.

이때, set 함수를 이용해 input으로 받는 문자열을 집합으로 만드는데, 이렇게 되면 result = 0 이 되는 조건이 발생할 수 없다.

N = int(input())
num = 0

def cal(x):
    store = []
    result = 1

    for i in range(0, len(x)):
        if x[i] in store:
            result = 0
        else:
            store.append(x[i])

    return result

while N>0:
    char = input()
    char = list(set(char))

    if cal(char):
        num += 1

    N -= 1

print(num)

 

[수정 전]

 

N = int(input())
cnt = N

while N>0:
    word = input()
    for i in range(len(word)-1):
        if word[i] == word[i+1]:
            pass
        elif word[i] in word[i+1:]:
            cnt -= 1
            break
    N -= 1

print(cnt)

[수정 후]

 

새로운 관점이 필요하다.

 관점 1. 조건에 해당될 때, 값을 빼는 것

 관점 2. 특정 조건을 통과했을 때, 다른 조건을 추가하여 두 가지 조건을 동시에 만족하는 것으로 조건 만들기.