Skip to main content

94. Group Shifted Strings

mediumAsked at Vercel

Group strings that are shifts of each other (caesar-cipher equivalent). Vercel asks this for the canonical-key + hash-map pattern with a custom equivalence relation — same shape as grouping shifted versions of the same edge-route signature.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-12)Vercel platform onsite; diff-pattern key expected.
  • LeetCode Discuss (2026-Q1)Listed in Vercel screen pool.

Problem

We can shift a string by shifting each of its letters to its successive letter. For example, 'abc' can be shifted to be 'bcd'. We can keep shifting the string to form a sequence. Given an array of strings strings, group all strings[i] that belong to the same shifting sequence. You may return the answer in any order.

Constraints

  • 1 <= strings.length <= 200
  • 1 <= strings[i].length <= 50
  • strings[i] consists of lowercase English letters.

Examples

Example 1

Input
strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
Output
[["acef"],["a","z"],["abc","bcd","xyz"],["az","ba"]]

Approaches

1. All-pairs shift comparison

For each pair, check shift equivalence; union-find.

Time
O(n^2 * L)
Space
O(n)
// Quadratic. Use diff-pattern key.

Tradeoff: Slow.

2. Diff-pattern key (optimal)

For each string, compute the diff between consecutive characters (mod 26). Strings in the same shifting sequence have the same diff pattern.

Time
O(n * L)
Space
O(n * L)
function groupStrings(strings) {
  const groups = new Map();
  for (const s of strings) {
    const diffs = [];
    for (let i = 1; i < s.length; i++) {
      diffs.push((s.charCodeAt(i) - s.charCodeAt(i - 1) + 26) % 26);
    }
    const key = diffs.join(',');
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(s);
  }
  return [...groups.values()];
}

Tradeoff: Single pass per string. The `+ 26) % 26` handles the wrap-around (z -> a is diff 1).

Vercel-specific tips

Vercel grades the diff-pattern key. Bonus signal: handling the wraparound (`+ 26) % 26` so `az` and `ba` group correctly — both have diff [25]). Single-character strings all share the empty diff pattern, grouping into one cluster.

Common mistakes

  • Forgetting the `+ 26) % 26` — negative diffs cause incorrect grouping.
  • Using char as key — only works on single-char strings.
  • Not handling length-1 strings — all should be in the same group with empty key.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Group Anagrams (LC 49) — same shape with sort-based key.
  • Group strings that are reverses of each other.
  • Detect if two strings are shifts of each other (single pair).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why mod 26?

Shifts wrap around the alphabet (z -> a is a +1 shift). Without mod, 'az' has diff [25] but 'ba' has diff [-25]; they should be equivalent. The mod normalizes to [25] for both.

Why diffs and not absolute positions?

Absolute positions change with every shift. Diffs are SHIFT-INVARIANT — exactly the equivalence relation we want.

Practice these live with InterviewChamp.AI

Drill Group Shifted Strings and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →