DIY: Group Shifted Strings
Solve the interview question "Group Shifted Strings" in this lesson.
We'll cover the following
Problem statement
Given a list 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 a list of strings. The following is an example input:
["acd", "dfg", "wyz", "yab", "mop", "bdfh", "b", "y", "moqs"]
Output
The output will be a list of grouped strings. The following is an example output:
[
["acd", "dfg" "wyz" "yab" "mop"]
["bdfh", "moqs"]
["b", "y"]
]
Coding exercise
Implement the group_strings(strs)
function, where strs
is the list of strings. The function will return a list of grouped strings within a list.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.