Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Group words with the same letters

    MediumAbout 25 minFull AccessLearn it: Hashing Patterns in C#: Dictionary, HashSet, CollectionsMarshal and Bad Hash Codes

    The task

    A word game refuses a new word when it only rearranges the letters of a word already played, so the server groups such words.

    Complete GroupSameLetters(string[] words). Words are lowercase. Put words that use the same letters the same number of times into one group. Groups appear in the order of their first word, and words keep their input order inside a group. Repeated words stay.

    Signature

    public static List<List<string>> GroupSameLetters(string[] words)

    Examples

    • ["tops", "spot", "cat", "post", "act", "dog"] → [["tops", "spot", "post"], ["cat", "act"], ["dog"]]. "spot" and "post" use the letters of "tops".
    • ["abc", "bcd", "cab"] → [["abc", "cab"], ["bcd"]]. "cab" joins the first group, which comes first because "abc" came first.

    Limits: Up to 30,000 words of up to 250 letters.

    The tests check: an empty list, one word, empty words, repeated words, words of different lengths, the same letters with different counts, words with no partner, and 30,000 words in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Groups words that use the same letters the same number of times, in first-seen order.
          public static List<List<string>> GroupSameLetters(string[] words)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.