Google Lyrics and Keyword Interview Problem
Anonymous User
904

I recently received this two-part onsite interview question from Google. The first part was already asked in this LeetCode post. Does anyone have a detailed solution for part 2 of this question?

Part 1
Write a function canMatch() that takes a string lyrics, and a string keyword. Return True if if the keyword is hidden in the lyrics. You may assume that each word in lyrics is separated by one space.

Each word in lyrics can only be used for one letter in the keyword. If lyrics = "boom" and keyword = "boom" , then canMatch() should return False because there would only be one match for "b".

Example:

lyrics = "every breath you take every move you make"
keyword = "boom"

 every (b)reath
      y(o)u take
every m(o)ve
   you (m)ake

output: True

Part 2
As a follow-up, write a function canMatch() that takes a string lyrics, a string keyword, and an int k. Return True if if the keyword is hidden within k words in the lyrics.

Example 1:

lyrics = "every breath you take every move you make"
keyword = "boom"
k = 5

output = False

Example 2:

lyrics = "every breath you take every move you make"
keyword = "boom"
k = 7

output = True

Does anyone have a detailed solution for part 2 of this question?

Comments (5)