Skip to content

Commit ce784d0

Browse files
committedApr 13, 2025
Add solution #1405
1 parent 36c71da commit ce784d0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-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,284 LeetCode solutions in JavaScript
1+
# 1,285 LeetCode solutions in JavaScript
22

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

@@ -1073,6 +1073,7 @@
10731073
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10741074
1403|[Minimum Subsequence in Non-Increasing Order](./solutions/1403-minimum-subsequence-in-non-increasing-order.js)|Easy|
10751075
1404|[Number of Steps to Reduce a Number in Binary Representation to One](./solutions/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js)|Medium|
1076+
1405|[Longest Happy String](./solutions/1405-longest-happy-string.js)|Medium|
10761077
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
10771078
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
10781079
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* 1405. Longest Happy String
3+
* https://leetcode.com/problems/longest-happy-string/
4+
* Difficulty: Medium
5+
*
6+
* A string s is called happy if it satisfies the following conditions:
7+
* - s only contains the letters 'a', 'b', and 'c'.
8+
* - s does not contain any of "aaa", "bbb", or "ccc" as a substring.
9+
* - s contains at most a occurrences of the letter 'a'.
10+
* - s contains at most b occurrences of the letter 'b'.
11+
* - s contains at most c occurrences of the letter 'c'.
12+
*
13+
* Given three integers a, b, and c, return the longest possible happy string. If there are
14+
* multiple longest happy strings, return any of them. If there is no such string, return
15+
* the empty string "".
16+
*
17+
* A substring is a contiguous sequence of characters within a string.
18+
*/
19+
20+
/**
21+
* @param {number} a
22+
* @param {number} b
23+
* @param {number} c
24+
* @return {string}
25+
*/
26+
var longestDiverseString = function(a, b, c) {
27+
const counts = [
28+
{ char: 'a', count: a },
29+
{ char: 'b', count: b },
30+
{ char: 'c', count: c }
31+
];
32+
const result = [];
33+
34+
while (true) {
35+
counts.sort((x, y) => y.count - x.count);
36+
37+
let added = false;
38+
for (let i = 0; i < 3; i++) {
39+
const { char, count } = counts[i];
40+
if (count === 0) continue;
41+
42+
const total = result.length;
43+
if (total >= 2 && result[total - 1] === char && result[total - 2] === char) {
44+
continue;
45+
}
46+
47+
result.push(char);
48+
counts[i].count--;
49+
added = true;
50+
break;
51+
}
52+
53+
if (!added) break;
54+
}
55+
56+
return result.join('');
57+
};

0 commit comments

Comments
 (0)
Please sign in to comment.