문제 풀이/Baekjoon Online Judge

[python3] 1316번 그룹 단어 체커

[잉간] 2019. 4. 9. 14:14


입력받은 문자열을 set 자료형을 사용해 중복을 제거해 c에 저장한다. 그리고 반복문을 사용해 c의 요소를 하나하나 비교한다. 

index()를 사용해 요소 i의 위치를 찾고, count()를 사용해 요소 i의 개수를 찾는다.

그 후 문자열 슬라이싱을 사용해 index부터 index + count까지의 문자열을 비교한다.

만약 요소 i와 문자열이 다를 때는 0을 반환하고 전부 일치할 때는 1을 반환한다. 

문제풀이

ans = 0
def check(words):
    c = set(words)
    for i in c:
        index = words.index(i)
        count = words.count(i)
        for rpt in words[index:index+count]:
            if i != rpt:
                return 0
    return 1
for rpt in range(int(input())):
    ans += check(input())
print(ans)