Google | Onsite | Form String A from suffix of String B

Create string A using suffix of string B. Return the minimum number of suffixes you have to use.

Example 1:

Input: A = "vvav", B = "vav"
Output: 2
Explanation:
String B has  suffixes: "v", "av", "vav"
A = "v" + "vav"

Example 2:

Input: A = "vavvavav", B = "vavav"
Output: 2
Explanation:
String B has suffixes: "v", "av", "vav", "avav", "vavav"
A = "vav" + "vavav"

Example 3:

Input: A = "cdcd", B = "dcd"
Output: 2
Explanation:
String B has suffixes: "d", "cd", "dcd"
A = "cd" + "cd"

If you create list of all suffix then I find this problem similar to WordBreak ii .
Rather than finding list of all combinations, we have to find combination with minimum number of elements.

Time Complexity:
Suffix of B can be created in O(n) where n is size of B
Then min number of suffix required can be found in O(m^2) where m is size of A
Hence O(n+m^2)

I do not know how suffix tree can be of help here ?

Comments (11)