|
| 1 | +/** |
| 2 | + * 2030. Smallest K-Length Subsequence With Occurrences of a Letter |
| 3 | + * https://leetcode.com/problems/smallest-k-length-subsequence-with-occurrences-of-a-letter/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given a string s, an integer k, a letter letter, and an integer repetition. |
| 7 | + * |
| 8 | + * Return the lexicographically smallest subsequence of s of length k that has the letter |
| 9 | + * letter appear at least repetition times. The test cases are generated so that the letter |
| 10 | + * appears in s at least repetition times. |
| 11 | + * |
| 12 | + * A subsequence is a string that can be derived from another string by deleting some or no |
| 13 | + * characters without changing the order of the remaining characters. |
| 14 | + * |
| 15 | + * A string a is lexicographically smaller than a string b if in the first position where a |
| 16 | + * and b differ, string a has a letter that appears earlier in the alphabet than the |
| 17 | + * corresponding letter in b. |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {string} s |
| 22 | + * @param {number} k |
| 23 | + * @param {character} letter |
| 24 | + * @param {number} repetition |
| 25 | + * @return {string} |
| 26 | + */ |
| 27 | +var smallestSubsequence = function(s, k, letter, repetition) { |
| 28 | + const n = s.length; |
| 29 | + let letterTotal = 0; |
| 30 | + for (let i = 0; i < n; i++) { |
| 31 | + if (s[i] === letter) letterTotal++; |
| 32 | + } |
| 33 | + |
| 34 | + const stack = []; |
| 35 | + let stackLetterCount = 0; |
| 36 | + let remainingCount = letterTotal; |
| 37 | + |
| 38 | + for (let i = 0; i < n; i++) { |
| 39 | + const char = s[i]; |
| 40 | + |
| 41 | + if (char === letter) { |
| 42 | + remainingCount--; |
| 43 | + } |
| 44 | + |
| 45 | + while ( |
| 46 | + stack.length > 0 && stack[stack.length - 1] > char && stack.length - 1 + (n - i) >= k |
| 47 | + && (stack[stack.length - 1] !== letter || stackLetterCount + remainingCount > repetition) |
| 48 | + ) { |
| 49 | + if (stack[stack.length - 1] === letter) { |
| 50 | + stackLetterCount--; |
| 51 | + } |
| 52 | + stack.pop(); |
| 53 | + } |
| 54 | + |
| 55 | + if (stack.length < k) { |
| 56 | + if (char === letter) { |
| 57 | + stackLetterCount++; |
| 58 | + } |
| 59 | + |
| 60 | + const neededLetters = repetition - stackLetterCount; |
| 61 | + const remainingPositions = k - stack.length - 1; |
| 62 | + if (char === letter || remainingPositions >= neededLetters) { |
| 63 | + stack.push(char); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (stackLetterCount < repetition) { |
| 69 | + let missingLetters = repetition - stackLetterCount; |
| 70 | + const result = [...stack]; |
| 71 | + |
| 72 | + for (let i = k - 1; i >= 0 && missingLetters > 0; i--) { |
| 73 | + if (result[i] !== letter) { |
| 74 | + result[i] = letter; |
| 75 | + missingLetters--; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return result.join(''); |
| 80 | + } |
| 81 | + |
| 82 | + return stack.join(''); |
| 83 | +}; |
0 commit comments