Word Appears Exactly N Times
Updated
Problem statement
You are given a string s, an integer n and a string word.
A word of s is a longest block of English letters (a to z, A to Z) standing next to each other. Every other character, including digits, spaces and punctuation, separates words. Two words are equal only if they have exactly the same letters in the same case.
Return true if word appears as a word of s exactly n times, and false otherwise. Copies of word inside a longer word do not count.
Function
appearsExactly(s: String, n: int, word: String) → booleanExamples
Example 1
s = "the cat, the hat; THE end"n = 2word = "the"return = trueThe words are the, cat, the, hat, THE and end. Exactly two of them are spelled the; THE has different letters (case matters). So the answer is true.
Example 2
s = "go,go-gone 2go"n = 2word = "go"return = falseThe comma, the dash, the space and the digit 2 all separate words, so the words are go, go, gone and go. gone is a different word, but go still appears 3 times, not 2. The answer is false.
Example 3
s = "...!!"n = 0word = "x"return = trueThe string has no letters, so it has no words. The word appears 0 times, which is exactly n = 0, so the answer is true.
Constraints
1 <= s.length <= 5000sconsists of English letters, digits, spaces and the characters, . ; : ! ? - ( ).0 <= n <= 50001 <= word.length <= 20, andwordconsists only of English letters.
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
- Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
- Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Rich Text Parser Part 1 (Validate Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Convert Hexadecimal to Dotted IPv4Microsoft · ONSITE INTERVIEWMicrosoft interview questions
- Message Latencies from CSV LogsAmazon · ONSITE INTERVIEW
- URL / Path Segment Compression Part 1Stripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Most Cost-Effective MachineCapital One · ONSITE INTERVIEWCapital One interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions