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] Minimum Distance Between Two Words 두 단어사이 최소거리 찾기. 본문

카테고리 없음

[Python/String-Manipulation] Minimum Distance Between Two Words 두 단어사이 최소거리 찾기.

kingmusung 2023. 11. 23. 00:03

문제

단어 리스트와 그 뒤에 이어지는 두 개의 단어가 주어졌을 때, 주어진 두 단어 사이의 최소 거리를 해당 단어 리스트에서 찾으세요.

입출력 예시)

Input: S = [ “Apple”, “Banana”, “Apple”, “Cherry”, “Grape”]

word1 = “Apple”

word2 = “Grape”

Output: 2

 

코드

S = ["Apple", "Grape", "Apple", "Cherry", "Grape"] 
word1 = "Apple"
word2 = "Grape"
distace=[]
count=0

for item in S:
    count+=1
    if item ==word1:
        count=0
    elif item ==word2:
        distace.append(count)

distace.sort()
        

print(distace.pop(0))