Alle Herausforderungen
Schwierig 0 gelöst 0% Erfolgsquote

Find Many Patterns in Text

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

Problem

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
text = "bananas"
patterns = ["ana", "apple", "nas"]

Erwartete Ausgabe:
expected = ["YES", "NO", "YES"]
Eingabe:
std::string text = "bananas";
std::vector<std::string> patterns = {"ana", "apple", "nas"};

Erwartete Ausgabe:
std::vector<std::string> expected = {"YES", "NO", "YES"};
Eingabe:
string text = "bananas";
string[] patterns = new string[] {"ana", "apple", "nas"};

Erwartete Ausgabe:
string[] expected = new string[] {"YES", "NO", "YES"};

Erläuterung

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

Beispiel 2
Eingabe:
text = "aaaa"
patterns = ["aa", "aaaaa", "aa"]

Erwartete Ausgabe:
expected = ["YES", "NO", "YES"]
Eingabe:
std::string text = "aaaa";
std::vector<std::string> patterns = {"aa", "aaaaa", "aa"};

Erwartete Ausgabe:
std::vector<std::string> expected = {"YES", "NO", "YES"};
Eingabe:
string text = "aaaa";
string[] patterns = new string[] {"aa", "aaaaa", "aa"};

Erwartete Ausgabe:
string[] expected = new string[] {"YES", "NO", "YES"};

Erläuterung

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.