Group Shift-Equivalent Strings
Updated
Problem statement
Two lowercase strings are shift-equivalent when they have the same length and you can turn one into the other by moving every letter forward by the same number of places in the alphabet. Moving forward past z wraps around to a.
Another way to check: for each pair of neighboring letters, count how many steps forward it takes to get from the first letter to the second, wrapping around the alphabet (so the count is taken modulo 26). Two strings of the same length are shift-equivalent when they get the same counts in the same order. For example, "aaa" and "zzz" are shift-equivalent, and so are "abc" and "bcd".
You are given strings. Put shift-equivalent strings in the same group and other strings in different groups. Return all the groups.
- Inside a group, keep the strings in the order they appear in the input.
- Order the groups by where each group's first string appears in the input.
- If a string appears more than once, keep every copy as its own entry.
Function
groupShiftEquivalentStrings(strings: String[]) → String[][]Examples
Example 1
strings = ["aaa","zzz","abc","bcd"]return = [["aaa","zzz"],["abc","bcd"]]In "aaa" and "zzz", every letter is the same as its neighbor, so every step count is zero. In "abc" and "bcd", every step count is 1. So the answer has two groups: "aaa" with "zzz", then "abc" with "bcd".
Example 2
strings = ["abc","bcd","acef","xyz","az","ba","a","z"]return = [["abc","bcd","xyz"],["acef"],["az","ba"],["a","z"]]In "abc", "bcd" and "xyz", each letter is one step after the one before it, so they form one group. No other string has the same pattern as "acef", so it is alone. In both "az" and "ba", the second letter is one place before the first in the alphabet, so their step counts match. The one-letter strings "a" and "z" have no neighboring letters to compare, so they match each other. The four groups are listed in order of first appearance.
Example 3
strings = ["a","a","b","aa","bb"]return = [["a","a","b"],["aa","bb"]]The two copies of "a" stay as separate entries. All one-letter strings are shift-equivalent, so "a", "a" and "b" form the first group. In "aa" and "bb", both letters are the same, so the step count is zero in each, and they form the second group.
Constraints
1 <= strings.length <= 100000.1 <= strings[i].length <= 100000.- Every string contains only lowercase English letters.
- The total number of characters is at most
100000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Detect Cross-Doctor Prescription Pairs Within 30 DaysOracle · ONSITE INTERVIEWOracle interview questions
- Invalid TransactionsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Group Shifted StringsMathWorks · ONSITE INTERVIEWMathWorks interview questions