[SWEA D3] 1213. [S/W 문제해결 기본] 3일차 - String

shin·2022년 11월 16일
0

CodingTest 문제 풀이

목록 보기
45/79

문제 : [SWEA D3] 1213. [S/W 문제해결 기본] 3일차 - String

풀이

for i in range(10):
    t = int(input())
    word = input()
    line = input()
    index = 0
    count = 0
    while True:
        index = line.find(word, index)
        if index == -1:
            break
        index += len(word)
        count += 1
    print(f"#{t} {count}")
  • line.find(word, index) : line의 index 자리부터 word가 있는지 확인, 있으면 해당 index 반환 없으면 -1 반환
  • 만약 -1을 반환하면 카운트를 종료함
  • 다음 탐색은 해당 index에서 word 길이만큼 더한 index부터 시작
profile
Backend development

0개의 댓글