380. Word Abbreviation
Given an array of distinct strings words, return the minimal possible abbreviations for every word.
The following are the rules for a string abbreviation:
- The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
- If more than one word shares the same abbreviation, then perform the following operation:
- Increase the prefix (characters in the first part) of each of their abbreviations byÂ
1. - For example, say you start with the wordsÂ
["abcdef","abndef"] both initially abbreviated asÂ"a4f". Then, a sequence of operations would beÂ["a4f","a4f"] ->Â["ab3f","ab3f"] ->Â["abc2f","abn2f"]. - This operation is repeated until every abbreviation is unique.
3. At the end, if an abbreviation did not make a word shorter, then keep it as the original word.
Example 1:
Input: words = ["like","god","internal","me","internet","interval","intension","face","intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
Example 2:
Input: words = ["aa","aaa"]
Output: ["aa","aaa"]
Now Your Turn!
Pick the correct output for the given inputInput: words = ["aa","aaa","abc","abcdffv"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
1 <= words.length <= 4002 <= words[i].length <= 400words[i]Â consists of lowercase English letters.- All the strings ofÂ
words are unique.