Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

JustDoEat

[Python/String-Manipulation] Check Rotation 본문

카테고리 없음

[Python/String-Manipulation] Check Rotation

kingmusung 2023. 11. 26. 19:42

문제

Given two words, check whether one word is a rotated version of another. 두개의 단어가 주어졌을 때, 하나의 단어가 다른 하나의 “회전된” 변형인지를 체크하세요

 

 

입출력 예시1:

Input: word1 = “Apple” , word2 = “leApp

입출력 예시2:

Input: word1 = “Apple” word2 = “ppleA"

Output: True

 

코드

word1 = "Aeplp"
word2 = "leApp"

word1_list = list(word1)
word2_list = list(word2)


dic={}
dic2={}

'''for char,char2 in zip(word1_list,word2_list):
    if char not in dic and char2 not in dic2:
        dic[char]=1
        dic2[char2]=1
    else:
        dic[char]+=1
        dic2[char2]+=1 #이렇게 하면 사전의 길이가 다르면 오류가 나올 수 있음
'''
#따로 조건문을 걸어주는게 좋음.
for char,char2 in zip(word1_list,word2_list):
    if char not in dic:
        dic[char] = 1
    else:
        dic[char] += 1

    if char2 not in dic2:
        dic2[char2] = 1
    else:
        dic2[char2] += 1

if dic == dic2:
    print("true")
else:
    print("False")

    
'''
for char,char2 in zip(word1_list,word2_list):
    dic[char]=dic.get(char,0)+1
    dic2[char2]=dic2.get(char2,0)+1
'''
"""
word1 = "Apple"
word2 = "leApp"
word1_list=list(word1)
word2_list=list(word2)
word1_list.sort()
word2_list.sort()

result = word1_list == word2_list
print(result)
"""