Skip to content

Commit a58fa8f

Browse files
committed
Add solution #411
1 parent b4c06b1 commit a58fa8f

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@
399399
408|[Valid Word Abbreviation](./solutions/0408-valid-word-abbreviation.js)|Easy|
400400
409|[Longest Palindrome](./solutions/0409-longest-palindrome.js)|Easy|
401401
410|[Split Array Largest Sum](./solutions/0410-split-array-largest-sum.js)|Hard|
402+
411|[Minimum Unique Word Abbreviation](./solutions/0411-minimum-unique-word-abbreviation.js)|Hard|
402403
412|[Fizz Buzz](./solutions/0412-fizz-buzz.js)|Easy|
403404
413|[Arithmetic Slices](./solutions/0413-arithmetic-slices.js)|Medium|
404405
414|[Third Maximum Number](./solutions/0414-third-maximum-number.js)|Easy|
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* 411. Minimum Unique Word Abbreviation
3+
* https://leetcode.com/problems/minimum-unique-word-abbreviation/
4+
* Difficulty: Hard
5+
*
6+
* A string can be abbreviated by replacing any number of non-adjacent substrings with their
7+
* lengths. For example, a string such as "substitution" could be abbreviated as (but not
8+
* limited to):
9+
* - "s10n" ("s ubstitutio n")
10+
* - "sub4u4" ("sub stit u tion")
11+
* - "12" ("substitution")
12+
* - "su3i1u2on" ("su bst i t u ti on")
13+
* - "substitution" (no substrings replaced)
14+
*
15+
* Note that "s55n" ("s ubsti tutio n") is not a valid abbreviation of "substitution" because
16+
* the replaced substrings are adjacent.
17+
*
18+
* The length of an abbreviation is the number of letters that were not replaced plus the
19+
* number of substrings that were replaced. For example, the abbreviation "s10n" has a length
20+
* of 3 (2 letters + 1 substring) and "su3i1u2on" has a length of 9 (6 letters + 3 substrings).
21+
*
22+
* Given a target string target and an array of strings dictionary, return an abbreviation
23+
* of target with the shortest possible length such that it is not an abbreviation of any
24+
* string in dictionary. If there are multiple shortest abbreviations, return any of them.
25+
*/
26+
27+
/**
28+
* @param {string} target
29+
* @param {string[]} dictionary
30+
* @return {string}
31+
*/
32+
var minAbbreviation = function(target, dictionary) {
33+
let minLength = target.length;
34+
let result = target;
35+
const validDict = dictionary.filter(word => word.length === target.length);
36+
37+
for (let mask = 0; mask < (1 << target.length); mask++) {
38+
const abbr = getAbbr(target, mask);
39+
if (abbr.length <= minLength) {
40+
let isValid = true;
41+
for (const word of validDict) {
42+
if (conflicts(abbr, word)) {
43+
isValid = false;
44+
break;
45+
}
46+
}
47+
if (isValid) {
48+
if (abbr.length < minLength) {
49+
minLength = abbr.length;
50+
result = abbr;
51+
} else if (abbr.length === minLength && abbr < result) {
52+
result = abbr;
53+
}
54+
}
55+
}
56+
}
57+
58+
return result;
59+
60+
function getAbbr(str, mask) {
61+
let abbr = '';
62+
let count = 0;
63+
for (let i = 0; i < str.length; i++) {
64+
if (mask & (1 << i)) {
65+
if (count) {
66+
abbr += count;
67+
count = 0;
68+
}
69+
abbr += str[i];
70+
} else {
71+
count++;
72+
}
73+
}
74+
if (count) abbr += count;
75+
return abbr;
76+
}
77+
78+
function conflicts(abbr, word) {
79+
let i = 0;
80+
let j = 0;
81+
while (i < abbr.length && j < word.length) {
82+
if (i < abbr.length && j < word.length && abbr[i] === word[j]) {
83+
i++;
84+
j++;
85+
} else if (i < abbr.length && /\d/.test(abbr[i])) {
86+
let num = 0;
87+
while (i < abbr.length && /\d/.test(abbr[i])) {
88+
num = num * 10 + Number(abbr[i++]);
89+
}
90+
j += num;
91+
} else {
92+
return false;
93+
}
94+
}
95+
return i === abbr.length && j === word.length;
96+
}
97+
};

0 commit comments

Comments
 (0)