Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5e5bab8

Browse files
committed
Add solution #2030
1 parent f0ab976 commit 5e5bab8

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,697 LeetCode solutions in JavaScript
1+
# 1,698 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1557,6 +1557,7 @@
15571557
2027|[Minimum Moves to Convert String](./solutions/2027-minimum-moves-to-convert-string.js)|Easy|
15581558
2028|[Find Missing Observations](./solutions/2028-find-missing-observations.js)|Medium|
15591559
2029|[Stone Game IX](./solutions/2029-stone-game-ix.js)|Medium|
1560+
2030|[Smallest K-Length Subsequence With Occurrences of a Letter](./solutions/2030-smallest-k-length-subsequence-with-occurrences-of-a-letter.js)|Hard|
15601561
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
15611562
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
15621563
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)