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

jeongjeong2·2023년 9월 23일
0

For coding test

목록 보기
58/59

문제 바로가기

문제 풀이

현재 단어와 다음 단어를 비교했을 때 같으면 pass, 다르면 list에 저장되어있는지 확인 후 저장되어있지 않으면 append, 이미 저장되어 있으면 cnt-=1하고 break

정답 코드

"""
https://www.acmicpc.net/problem/1316
"""
N = int(input())
cnt = N
for _ in range(N):
    word = input()
    l = []
    for idx in range(len(word)):
        if idx == len(word) - 1:
            if word[idx] in l:
                cnt -= 1
        elif word[idx] == word[idx + 1]:
            pass
        else:
            if word[idx] not in l:
                l.append(word[idx])
            else:
                cnt -= 1
                break
print(cnt)

다른 사람 코드 (optional)

N = int(input())
cnt = N

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

print(cnt)

string에 대해서 slicing으로 문자열 일부를 자른 후 그 안에 현재의 원소가 들어있는지 판단. in/not in은 반드시 list에서만 쓰는 것이 아닌 문자열에도 사용할 수 있다.

0개의 댓글