본문 바로가기
Algorithm(CodeTree, Python)/완전탐색3

[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 등장하지 않는 문자열의 길이 Python

by kurooru 2022. 12. 29.
# n 입력
n = int(input())
# string 입력
string = input()

# 함수들
# exists_more_than_two_times(search_str)
def exists_more_than_two_times(search_str):

    # 검색하는 문자열 중
    for s in search_str:
        
        # curr_cnt
        curr_cnt = 0
        
        # 같은 문자열 내에서
        for another_s in search_str:
            # 자신과 같은 문자열을 발견하면
            if s == another_s:
                # curr_cnt 올려주기
                curr_cnt += 1
        
        # 한번이라도 두 번 이상 있었으면
        if curr_cnt >= 2:
            # 성공
            return True
    
    # 없었으면 실패
    return False

# get_curr_str(len_of_str)
def get_curr_str(len_of_str):
    
    # curr_list
    curr_list = []

    # 현재의 길이만큼만 잘라
    for i in range(n - len_of_str + 1):
        # curr_list에 추가해주고,
        curr_list.append(string[i:i+len_of_str])
    
    # 반환
    return curr_list

# 설계
# ans
ans = 0

# 길이를 기준으로 완전탐색
for i in range(1, n+1):
    
    # curr_str -> 현재 길이의 탐색할 문자열들을 담아 줄 리스트
    curr_str = get_curr_str(i)

    # curr_str 내의 단어들이 string 내에서 두 번 이상 등장하지 않으면
    if not exists_more_than_two_times(curr_str):
        # ans update
        ans = i
        # break
        break

# 출력
print(ans)