Skip to content

Commit 36d69ce

Browse files
committed
Sync LeetCode submission Runtime - 63 ms (61.84%), Memory - 18.5 MB (94.55%)
1 parent 19fa5d6 commit 36d69ce

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

0527-word-abbreviation/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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>[&quot;abcdef&quot;,&quot;abndef&quot;]</code> both initially abbreviated as <code>&quot;a4f&quot;</code>. Then, a sequence of operations would be <code>[&quot;a4f&quot;,&quot;a4f&quot;]</code> -&gt; <code>[&quot;ab3f&quot;,&quot;ab3f&quot;]</code> -&gt; <code>[&quot;abc2f&quot;,&quot;abn2f&quot;]</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>&nbsp;</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>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= words.length &lt;= 400</code></li>
33+
<li><code>2 &lt;= words[i].length &lt;= 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>

0527-word-abbreviation/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def wordsAbbreviation(self, words: List[str]) -> List[str]:
3+
def abbrev(word, i = 0):
4+
if (len(word) - i <= 3):
5+
return word
6+
return word[:i+1] + str(len(word) - i - 2) + word[-1]
7+
8+
N = len(words)
9+
ans = list(map(abbrev, words))
10+
prefix = [0] * N
11+
12+
for i in range(N):
13+
while True:
14+
dupes = set()
15+
for j in range(i+1, N):
16+
if ans[i] == ans[j]:
17+
dupes.add(j)
18+
19+
if not dupes:
20+
break
21+
dupes.add(i)
22+
for k in dupes:
23+
prefix[k] += 1
24+
ans[k] = abbrev(words[k], prefix[k])
25+
26+
return ans

0 commit comments

Comments
 (0)