The Group Shifted Strings interview question asks you to group an array of strings based on their "shifting" property. A string can be shifted by moving every character forward in the alphabet by the same amount (e.g., "abc" shifted by 1 is "bcd", shifted by 25 is "zab"). Strings that can be shifted to match each other belong in the same group.
Companies like Uber and Google ask this Hash Table and String coding problem to test your ability to define a mathematical "signature" for a string. It’s a step up from "Group Anagrams." It evaluates whether you can use modular arithmetic to calculate the relative differences between characters and use that sequence of differences as a dictionary key.
This problem relies on a Relative Offset Hashing pattern.
2,3.2,3.Strings: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"]
"1,1"."1,1". (Groups with "abc")."25".(-1 + 26) % 26 = 25. Key: "25". (Groups with "az")."". (Group together).char[i] - char[i-1], the result can be negative if the string wraps around 'z' to 'a' (like "ba"). You must handle this using modulo 26: (diff + 26) % 26.When dealing with circular alphabets or clocks, the formula (value + modulo) % modulo is the safest way to ensure you always get a positive relative distance.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Word Subsets | Medium | Solve | |
| Evaluate the Bracket Pairs of a String | Medium | Solve | |
| Find Duplicate File in System | Medium | Solve | |
| People Whose List of Favorite Companies Is Not a Subset of Another List | Medium | Solve | |
| Vowel Spellchecker | Medium | Solve |