[BOJ/백준] 1316. 그룹 단어 체커(Python)

장성범·2022년 2월 3일
0

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

Problem

연속되지 않은 문자가 있는지 찾는문제

Solution

chkList[아스키코드]값을 통해 체크 로직 설정
1)첫번째 문자값은 1로 체크
2)두번째 문자부터 전값과 같고(연속) 이미 chkList에 있으면 continue
3)전값과 다르고 chkList에 없으면 chkList를 1로 만들기
4)전값과 다르고 chkList에 있으면,즉 이미 한번 나왔으면 그룹단어가 아님.

Python Code

import sys

N=int(sys.stdin.readline())

def groupChk(arr):
    chkList=[0 for i in range(ord('z')+1)]
    chkList[ord(arr[0])]=1

    chkFlag=True
    for i in range(1,len(arr)):
        if arr[i]==arr[i-1] and chkList[ord(arr[i])]==1:
            continue
        elif arr[i]!=arr[i-1] and chkList[ord(arr[i])]==0:
            chkList[ord(arr[i])]=1
        elif arr[i]!=arr[i-1] and chkList[ord(arr[i])]==1:
            chkFlag=False
            break
    return chkFlag

cnt = 0
for _ in range(N):
    arr=sys.stdin.readline()
    if(groupChk(arr)==True):
        cnt+=1
print(cnt)


0개의 댓글

관련 채용 정보