Skip to content

Commit df55ff1

Browse files
add 1415
1 parent 43d578c commit df55ff1

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1415|[The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1415.java) | |Medium|Backtracking|
1112
|1413|[Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1413.java) | |Easy|Array|
1213
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack|
1314
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1409.java) | |Medium|Array|
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1415. The k-th Lexicographical String of All Happy Strings of Length n
8+
*
9+
* A happy string is a string that:
10+
* consists only of letters of the set ['a', 'b', 'c'].
11+
* s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
12+
*
13+
* For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
14+
* Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
15+
* Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
16+
*
17+
* Example 1:
18+
* Input: n = 1, k = 3
19+
* Output: "c"
20+
* Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
21+
*
22+
* Example 2:
23+
* Input: n = 1, k = 4
24+
* Output: ""
25+
* Explanation: There are only 3 happy strings of length 1.
26+
*
27+
* Example 3:
28+
* Input: n = 3, k = 9
29+
* Output: "cab"
30+
* Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
31+
*
32+
* Example 4:
33+
* Input: n = 2, k = 7
34+
* Output: ""
35+
*
36+
* Example 5:
37+
* Input: n = 10, k = 100
38+
* Output: "abacbabacb"
39+
*
40+
* Constraints:
41+
* 1 <= n <= 10
42+
* 1 <= k <= 100
43+
* */
44+
public class _1415 {
45+
public static class Solution1 {
46+
public String getHappyString(int n, int k) {
47+
char[] chars = new char[]{'a', 'b', 'c'};
48+
List<String> happyStrings = new ArrayList<>();
49+
happyStrings.add("");
50+
happyStrings = findAllHappyStrings(chars, n, happyStrings);
51+
return happyStrings.size() < k ? "" : happyStrings.get(k - 1);
52+
}
53+
54+
private List<String> findAllHappyStrings(char[] chars, int n, List<String> happyStrings) {
55+
if (happyStrings.get(0).length() == n) {
56+
return happyStrings;
57+
}
58+
List<String> newHappyStrings = new ArrayList<>();
59+
for (String str : happyStrings) {
60+
for (char c : chars) {
61+
if (str.equals("") || str.charAt(str.length() - 1) != c) {
62+
StringBuilder newSb = new StringBuilder(str);
63+
newSb.append(c);
64+
newHappyStrings.add(newSb.toString());
65+
}
66+
}
67+
}
68+
happyStrings = newHappyStrings;
69+
return findAllHappyStrings(chars, n, happyStrings);
70+
}
71+
}
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1415;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1415Test {
10+
11+
private static _1415.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1415.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals("cab", solution1.getHappyString(3, 9));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)