String Formation (Also for AI/ML Software Engineer Intern :)
See Snowflake hiring insightsGiven an array of strings, each with the same length, and a target string, create the target string using characters from the strings in the given array such that the indices of the characters form a strictly increasing sequence. The index of a character is its position within the string, and multiple characters from the same string can be used.
Determine the number of ways to form the target string. Each construction is different if the sequences of indices used are different or if the sequences are the same but the characters are chosen from different strings at any index. Since the answer can be very large, return the result modulo 10^9 + 7.
Complete the function numWays in the editor with the following parameters:
string words[n]: an array of stringsstring target: the target string
Returns
int: the number of ways, modulo 10^9 + 7
words = ["valya", "lyglb", "vldoh"] target = "val" return = 4
There are 4 ways to construct the string val such that the indices will be in strictly increasing order.
- Select the 1st character of
valya, the 2nd character ofvalya, and the 3rd character ofvalya. - Select the 1st character of
valya, the 2nd character ofvalya, and the 4th character oflyglb. - Select the 1st character of
valya, the 2nd character ofvalya, and the 4th character ofvldoh. - Select the 1st character of
vldoh, the 2nd character ofvalya, and the 3rd character ofvalya.
words = ["adc", "aec", "efg"] target = "ac" return = 4
There are 4 ways to reach the target:
- Select the 1st character of
adcand the 3rd character ofadc. - Select the 1st character of
adcand the 3rd character ofaec. - Select the 1st character of
aecand the 3rd character ofadc. - Select the 1st character of
aecand the 3rd character ofaec.
1 <= n <= 10^31 <= length of words[i] <= 3000- All
words[i]are of equal length per test case. - The sum of the lengths of all
words[i]is<= 10^5. 1 <= length of target <= length of words[i]- All characters are lowercase English letters.
- Closest Target CharacterPHONE SCREEN · Seen Jul 2026
- Horizontal Pod AutoscalerSeen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringSeen Jun 2026
- Drawing EdgeOA · Seen Jun 2026
- Effective Role PrivilegesPHONE SCREEN · Seen May 2026
- Closest Bathroom / Desk on a GridPHONE SCREEN · Seen May 2026
- Minimum Clicks Between Wiki PagesOA · Seen May 2026
public int numWays(String[] words, String target) {
// write your code here
}