DIY: Group Shifted Strings

Solve the interview question "Group Shifted Strings" in this lesson.

Problem statement

Given an array of strings (lowercase alphabets), we have to group all strings that are shifted versions of each other. For a string abc, its shifted sequence can be lmn because each character in both strings gets incremented by 1 to form the next character. Similarly, any three-letter word that has a character difference of 1 between their letters will be shifted a sequence of abc.

Input

The input will be an array of strings. The following is an example input:

{"acd", "dfg", "wyz", "yab", "mop", "bdfh", "b", "y", "moqs"}

Output

The output will be an array of grouped strings. The following is an example output:

{
{"acd", "dfg" "wyz" "yab" "mop"}
{"bdfh", "moqs"}
{"b", "y"}
}

Coding exercise

Implement the groupStrings(strs) function, where strs is the array of strings. The function will return an array of grouped strings within an array.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.