FastPrepDetect a Keyword Substring
Problem · String

Detect a Keyword Substring

Learn this problem
EasyAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Given a string text and an array of nonempty keywords, return true when at least one keyword occurs as a contiguous substring of text.

For this exercise, assume matching is case-sensitive and literal. An empty keyword array produces false.

Function

containsKeyword(text: String, keywords: String[]) → boolean

Examples

Example 1

text = "distributed systems interview"keywords = ["system","database"]return = true

The keyword system occurs inside systems.

Example 2

text = "Cloud"keywords = ["cloud","database"]return = false

Matching is case-sensitive, so cloud does not match Cloud.

Constraints

  • 0 <= text.length <= 10^5
  • 0 <= keywords.length <= 10^4
  • 1 <= keywords[i].length <= 10^4
  • The total keyword length is at most 10^5.

More Amazon problems

drafts saved locally
public boolean containsKeyword(String text, String[] keywords) {
    // Your code here
}
text"distributed systems interview"
keywords["system","database"]
expectedtrue
checking account