Find dictionary words that can be spelled using every letter ring exactly once

Quick Overview

Given letter rings that each show one letter and a word list, return the sorted words that can be spelled by using every ring exactly once in any position. Tests modeling the check as a matching between word positions and rings, and handling repeated letters and ring reordering.

Find dictionary words that can be spelled using every letter ring exactly once

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of `rings`. Each ring is a string of lowercase letters, and it can be rotated to show any one of its letters. You are also given a list of `words` (a dictionary). A word can be formed when every ring is used exactly once, each ring shows one letter, and the shown letters spell the word. A ring can be placed at any position in the word: the order of `rings` does not fix which position a ring fills. Return every word in `words` that can be formed, sorted in lexicographical order. ### Function Signature ```python def word_rings(rings: list[str], words: list[str]) -> list[str]: ``` ### Rules - Every word has length `len(rings)`, so each position of the word gets exactly one ring and every ring is used. - A letter that appears several times in a word needs that many different rings that can show it. - If a word that can be formed appears more than once in `words`, each occurrence appears in the output. - Sort with ordinary string comparison (the order Python's `sorted` gives for lowercase strings). ### Constraints - `1 <= len(rings) <= 10` - `1 <= len(rings[i]) <= 26` - `1 <= len(words) <= 1000` - All `rings[i]` and `words[j]` consist of lowercase English letters. - Every word in `words` has length equal to `len(rings)`. ### Examples ```text Input: rings = ["abc","def","ghi"], words = ["adg","beh","cfi","abc","xyz","aaa"] Output: ["adg","beh","cfi"] ``` Each of "adg", "beh" and "cfi" takes one letter from each ring. "abc" fails because the second and third rings show none of a, b and c, "xyz" uses letters that are on no ring, and "aaa" needs three rings that show 'a' while only the first ring does. ```text Input: rings = ["ab","ab"], words = ["aa","ab","ba","bb"] Output: ["aa","ab","ba","bb"] ``` Both rings show 'a' and 'b', so every two-letter word over a and b works. A repeated letter is fine here because the two copies come from two different rings. ```text Input: rings = ["ab","a"], words = ["ba","bb","ab","aa","ba"] Output: ["aa","ab","ba","ba"] ``` For "ab", the second ring shows 'a' in the first position and the first ring shows 'b' in the second position. "bb" fails because only the first ring can show 'b'. "ba" appears twice in the input, so it appears twice in the output.

Overview: Given letter rings that each show one letter and a word list, return the sorted words that can be spelled by using every ring exactly once in any position. Tests modeling the check as a matching between word positions and rings, and handling repeated letters and ring reordering.

Snapchat logo
Snapchat
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of rings. Each ring is a string of lowercase letters, and it can be rotated to show any one of its letters. You are also given a list of words (a dictionary).

A word can be formed when every ring is used exactly once, each ring shows one letter, and the shown letters spell the word. A ring can be placed at any position in the word: the order of rings does not fix which position a ring fills.

Return every word in words that can be formed, sorted in lexicographical order.

Function Signature

def word_rings(rings: list[str], words: list[str]) -> list[str]:

Rules

  • Every word has length len(rings) , so each position of the word gets exactly one ring and every ring is used.
  • A letter that appears several times in a word needs that many different rings that can show it.
  • If a word that can be formed appears more than once in words , each occurrence appears in the output.
  • Sort with ordinary string comparison (the order Python's sorted gives for lowercase strings).

Constraints

  • 1 <= len(rings) <= 10
  • 1 <= len(rings[i]) <= 26
  • 1 <= len(words) <= 1000
  • All rings[i] and words[j] consist of lowercase English letters.
  • Every word in words has length equal to len(rings) .

Examples

Input: rings = ["abc","def","ghi"], words = ["adg","beh","cfi","abc","xyz","aaa"]
Output: ["adg","beh","cfi"]

Each of "adg", "beh" and "cfi" takes one letter from each ring. "abc" fails because the second and third rings show none of a, b and c, "xyz" uses letters that are on no ring, and "aaa" needs three rings that show 'a' while only the first ring does.

Input: rings = ["ab","ab"], words = ["aa","ab","ba","bb"]
Output: ["aa","ab","ba","bb"]

Both rings show 'a' and 'b', so every two-letter word over a and b works. A repeated letter is fine here because the two copies come from two different rings.

Input: rings = ["ab","a"], words = ["ba","bb","ab","aa","ba"]
Output: ["aa","ab","ba","ba"]

For "ab", the second ring shows 'a' in the first position and the first ring shows 'b' in the second position. "bb" fails because only the first ring can show 'b'. "ba" appears twice in the input, so it appears twice in the output.