|
| 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