모든 챌린지
어려움 1명 해결 정답률 100.0%

Find Many Patterns in Text

For each requested pattern, decide whether it appears as a contiguous substring anywhere in the shared text.

배우게 될 내용

  • Check many patterns against one text quickly
  • Preprocess text or patterns for fast lookups
  • Compare naive search with indexed search

문제

번역본 보기

For each requested pattern, decide whether it appears as a contiguous substring anywhere in the shared text. Patterns may overlap and may repeat. Return results in the original query order. The limits require an approach that can handle many patterns efficiently.

Implement find_many_patterns(text, patterns).

Input

text: The text searched by every query.

patterns: The patterns, in query order.

Output

Return YES for each occurring pattern and NO for each absent one.

제약 조건

1…100,000 text characters. 1…100,000 nonempty patterns, with total length at most 500,000. All characters are lowercase a–z.

예시

예시 1
입력:
text = "bananas"
patterns = ["ana", "apple", "nas"]

예상 출력:
expected = ["YES", "NO", "YES"]
입력:
std::string text = "bananas";
std::vector<std::string> patterns = {"ana", "apple", "nas"};

예상 출력:
std::vector<std::string> expected = {"YES", "NO", "YES"};
입력:
string text = "bananas";
string[] patterns = new string[] {"ana", "apple", "nas"};

예상 출력:
string[] expected = new string[] {"YES", "NO", "YES"};

설명

Ana appears starting at position 2 and nas at position 5 (positions start at 1 here). Apple never appears.

예시 2
입력:
text = "aaaa"
patterns = ["aa", "aaaaa", "aa"]

예상 출력:
expected = ["YES", "NO", "YES"]
입력:
std::string text = "aaaa";
std::vector<std::string> patterns = {"aa", "aaaaa", "aa"};

예상 출력:
std::vector<std::string> expected = {"YES", "NO", "YES"};
입력:
string text = "aaaa";
string[] patterns = new string[] {"aa", "aaaaa", "aa"};

예상 출력:
string[] expected = new string[] {"YES", "NO", "YES"};

설명

Aa occurs despite overlapping matches. Five a characters cannot fit in a four-character text. The repeated aa query receives the same answer.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Many patterns can share work, so avoid searching the text separately for each query.

힌트 2

Build one structure that stores all patterns and scan the text once, advancing matches as characters arrive.

힌트 3

Be careful to report repeated patterns in query order, and let overlapping matches count independently.

예상 복잡도

O(text length + total pattern length) time, O(total pattern length) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 848 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.