Skip to content

Commit 05bf433

Browse files
committed
feat: add solutions to lc problem: No.1415
No.1415.The k-th Lexicographical String of All Happy Strings of Length n
1 parent 25c3258 commit 05bf433

File tree

7 files changed

+198
-2
lines changed

7 files changed

+198
-2
lines changed

solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class Solution:
7373
return max(1, 1 - t)
7474
```
7575

76+
```python
77+
class Solution:
78+
def minStartValue(self, nums: List[int]) -> int:
79+
s = list(accumulate(nums))
80+
return 1 if min(s) >= 0 else abs(min(s)) + 1
81+
```
82+
7683
### **Java**
7784

7885
<!-- 这里可写当前语言的特殊实现逻辑 -->

solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class Solution:
6565
return max(1, 1 - t)
6666
```
6767

68+
```python
69+
class Solution:
70+
def minStartValue(self, nums: List[int]) -> int:
71+
s = list(accumulate(nums))
72+
return 1 if min(s) >= 0 else abs(min(s)) + 1
73+
```
74+
6875
### **Java**
6976

7077
```java

solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,85 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:DFS**
73+
7274
<!-- tabs:start -->
7375

7476
### **Python3**
7577

7678
<!-- 这里可写当前语言的特殊实现逻辑 -->
7779

7880
```python
79-
81+
class Solution:
82+
def getHappyString(self, n: int, k: int) -> str:
83+
def dfs(t):
84+
if len(t) == n:
85+
ans.append(t)
86+
return
87+
for c in 'abc':
88+
if t and t[-1] == c:
89+
continue
90+
dfs(t + c)
91+
92+
ans = []
93+
dfs('')
94+
return '' if len(ans) < k else ans[k - 1]
8095
```
8196

8297
### **Java**
8398

8499
<!-- 这里可写当前语言的特殊实现逻辑 -->
85100

86101
```java
102+
class Solution {
103+
private List<String> ans = new ArrayList<>();
104+
105+
public String getHappyString(int n, int k) {
106+
dfs("", n);
107+
return ans.size() < k ? "" : ans.get(k - 1);
108+
}
109+
110+
private void dfs(String t, int n) {
111+
if (t.length() == n) {
112+
ans.add(t);
113+
return;
114+
}
115+
for (char c : "abc".toCharArray()) {
116+
if (t.length() > 0 && t.charAt(t.length() - 1) == c) {
117+
continue;
118+
}
119+
dfs(t + c, n);
120+
}
121+
}
122+
}
123+
```
87124

125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
vector<string> ans;
131+
string getHappyString(int n, int k) {
132+
dfs("", n);
133+
return ans.size() < k ? "" : ans[k - 1];
134+
}
135+
136+
void dfs(string t, int n) {
137+
if (t.size() == n)
138+
{
139+
ans.push_back(t);
140+
return;
141+
}
142+
for (int c = 'a'; c <= 'c'; ++c)
143+
{
144+
if (t.size() && t.back() == c) continue;
145+
t.push_back(c);
146+
dfs(t, n);
147+
t.pop_back();
148+
}
149+
}
150+
};
88151
```
89152

90153
### **...**

solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,74 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def getHappyString(self, n: int, k: int) -> str:
62+
def dfs(t):
63+
if len(t) == n:
64+
ans.append(t)
65+
return
66+
for c in 'abc':
67+
if t and t[-1] == c:
68+
continue
69+
dfs(t + c)
70+
71+
ans = []
72+
dfs('')
73+
return '' if len(ans) < k else ans[k - 1]
6174
```
6275

6376
### **Java**
6477

6578
```java
79+
class Solution {
80+
private List<String> ans = new ArrayList<>();
81+
82+
public String getHappyString(int n, int k) {
83+
dfs("", n);
84+
return ans.size() < k ? "" : ans.get(k - 1);
85+
}
86+
87+
private void dfs(String t, int n) {
88+
if (t.length() == n) {
89+
ans.add(t);
90+
return;
91+
}
92+
for (char c : "abc".toCharArray()) {
93+
if (t.length() > 0 && t.charAt(t.length() - 1) == c) {
94+
continue;
95+
}
96+
dfs(t + c, n);
97+
}
98+
}
99+
}
100+
```
66101

102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
vector<string> ans;
108+
string getHappyString(int n, int k) {
109+
dfs("", n);
110+
return ans.size() < k ? "" : ans[k - 1];
111+
}
112+
113+
void dfs(string t, int n) {
114+
if (t.size() == n)
115+
{
116+
ans.push_back(t);
117+
return;
118+
}
119+
for (int c = 'a'; c <= 'c'; ++c)
120+
{
121+
if (t.size() && t.back() == c) continue;
122+
t.push_back(c);
123+
dfs(t, n);
124+
t.pop_back();
125+
}
126+
}
127+
};
67128
```
68129

69130
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<string> ans;
4+
string getHappyString(int n, int k) {
5+
dfs("", n);
6+
return ans.size() < k ? "" : ans[k - 1];
7+
}
8+
9+
void dfs(string t, int n) {
10+
if (t.size() == n)
11+
{
12+
ans.push_back(t);
13+
return;
14+
}
15+
for (int c = 'a'; c <= 'c'; ++c)
16+
{
17+
if (t.size() && t.back() == c) continue;
18+
t.push_back(c);
19+
dfs(t, n);
20+
t.pop_back();
21+
}
22+
}
23+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
private List<String> ans = new ArrayList<>();
3+
4+
public String getHappyString(int n, int k) {
5+
dfs("", n);
6+
return ans.size() < k ? "" : ans.get(k - 1);
7+
}
8+
9+
private void dfs(String t, int n) {
10+
if (t.length() == n) {
11+
ans.add(t);
12+
return;
13+
}
14+
for (char c : "abc".toCharArray()) {
15+
if (t.length() > 0 && t.charAt(t.length() - 1) == c) {
16+
continue;
17+
}
18+
dfs(t + c, n);
19+
}
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def getHappyString(self, n: int, k: int) -> str:
3+
def dfs(t):
4+
if len(t) == n:
5+
ans.append(t)
6+
return
7+
for c in 'abc':
8+
if t and t[-1] == c:
9+
continue
10+
dfs(t + c)
11+
12+
ans = []
13+
dfs('')
14+
return '' if len(ans) < k else ans[k - 1]

0 commit comments

Comments
 (0)