Skip to content

Commit ba18ec5

Browse files
committed
Add solution #2182
1 parent c05bdca commit ba18ec5

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,7 @@
19791979
2179|[Count Good Triplets in an Array](./solutions/2179-count-good-triplets-in-an-array.js)|Hard|
19801980
2180|[Count Integers With Even Digit Sum](./solutions/2180-count-integers-with-even-digit-sum.js)|Easy|
19811981
2181|[Merge Nodes in Between Zeros](./solutions/2181-merge-nodes-in-between-zeros.js)|Medium|
1982+
2182|[Construct String With Repeat Limit](./solutions/2182-construct-string-with-repeat-limit.js)|Medium|
19821983
2183|[Count Array Pairs Divisible by K](./solutions/2183-count-array-pairs-divisible-by-k.js)|Hard|
19831984
2184|[Number of Ways to Build Sturdy Brick Wall](./solutions/2184-number-of-ways-to-build-sturdy-brick-wall.js)|Medium|
19841985
2185|[Counting Words With a Given Prefix](./solutions/2185-counting-words-with-a-given-prefix.js)|Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 2182. Construct String With Repeat Limit
3+
* https://leetcode.com/problems/construct-string-with-repeat-limit/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s and an integer repeatLimit. Construct a new string
7+
* repeatLimitedString using the characters of s such that no letter appears more than
8+
* repeatLimit times in a row. You do not have to use all characters from s.
9+
*
10+
* Return the lexicographically largest repeatLimitedString possible.
11+
*
12+
* A string a is lexicographically larger than a string b if in the first position where
13+
* a and b differ, string a has a letter that appears later in the alphabet than the
14+
* corresponding letter in b. If the first min(a.length, b.length) characters do not differ,
15+
* then the longer string is the lexicographically larger one.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @param {number} repeatLimit
21+
* @return {string}
22+
*/
23+
var repeatLimitedString = function(s, repeatLimit) {
24+
const frequency = new Array(26).fill(0);
25+
for (const char of s) {
26+
frequency[char.charCodeAt(0) - 97]++;
27+
}
28+
29+
const result = [];
30+
let currentChar = 25;
31+
32+
while (currentChar >= 0) {
33+
if (frequency[currentChar] === 0) {
34+
currentChar--;
35+
continue;
36+
}
37+
38+
const useCount = Math.min(frequency[currentChar], repeatLimit);
39+
for (let i = 0; i < useCount; i++) {
40+
result.push(String.fromCharCode(currentChar + 97));
41+
}
42+
frequency[currentChar] -= useCount;
43+
44+
if (frequency[currentChar] > 0) {
45+
let nextChar = currentChar - 1;
46+
while (nextChar >= 0 && frequency[nextChar] === 0) {
47+
nextChar--;
48+
}
49+
50+
if (nextChar < 0) break;
51+
52+
result.push(String.fromCharCode(nextChar + 97));
53+
frequency[nextChar]--;
54+
} else {
55+
currentChar--;
56+
}
57+
}
58+
59+
return result.join('');
60+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy