bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 527. Word Abbreviation

527. Word Abbreviation

Leetcode Solutions

Approach #: Greedy

  1. Initialize an array abbreviations to store the initial abbreviations of the words.
  2. For each word, create its initial abbreviation and store it in abbreviations.
  3. Initialize a dictionary prefix to keep track of the prefix length for each word.
  4. While there are duplicates in abbreviations, perform the following steps: a. Create a dictionary groups to group words by their abbreviations. b. For each group with more than one word, increment the prefix length for those words in prefix. c. Update the abbreviations for the words with increased prefix length.
  5. After resolving all duplicates, replace any abbreviation that is not shorter than the original word with the original word itself.
  6. Return the final list of abbreviations.
UML Thumbnail

Approach #: Group + Least Common Prefix

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...