|
| 1 | +<p>Given an array of <strong>distinct</strong> strings <code>words</code>, return <em>the minimal possible <strong>abbreviations</strong> for every word</em>.</p> |
| 2 | + |
| 3 | +<p>The following are the rules for a string abbreviation:</p> |
| 4 | + |
| 5 | +<ol> |
| 6 | + <li>The <strong>initial</strong> abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.</li> |
| 7 | + <li>If more than one word shares the <strong>same</strong> abbreviation, then perform the following operation: |
| 8 | + <ul> |
| 9 | + <li><strong>Increase</strong> the prefix (characters in the first part) of each of their abbreviations by <code>1</code>. |
| 10 | + <ul> |
| 11 | + <li>For example, say you start with the words <code>["abcdef","abndef"]</code> both initially abbreviated as <code>"a4f"</code>. Then, a sequence of operations would be <code>["a4f","a4f"]</code> -> <code>["ab3f","ab3f"]</code> -> <code>["abc2f","abn2f"]</code>.</li> |
| 12 | + </ul> |
| 13 | + </li> |
| 14 | + <li>This operation is repeated until every abbreviation is <strong>unique</strong>.</li> |
| 15 | + </ul> |
| 16 | + </li> |
| 17 | + <li>At the end, if an abbreviation did not make a word shorter, then keep it as the original word.</li> |
| 18 | +</ol> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong class="example">Example 1:</strong></p> |
| 22 | +<pre><strong>Input:</strong> words = ["like","god","internal","me","internet","interval","intension","face","intrusion"] |
| 23 | +<strong>Output:</strong> ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"] |
| 24 | +</pre><p><strong class="example">Example 2:</strong></p> |
| 25 | +<pre><strong>Input:</strong> words = ["aa","aaa"] |
| 26 | +<strong>Output:</strong> ["aa","aaa"] |
| 27 | +</pre> |
| 28 | +<p> </p> |
| 29 | +<p><strong>Constraints:</strong></p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li><code>1 <= words.length <= 400</code></li> |
| 33 | + <li><code>2 <= words[i].length <= 400</code></li> |
| 34 | + <li><code>words[i]</code> consists of lowercase English letters.</li> |
| 35 | + <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> |
| 36 | +</ul> |
0 commit comments