Skip to content

Commit a267360

Browse files
add 1374
1 parent ba35823 commit a267360

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-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+
|1374|[Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1374.java) | |Easy||String
1112
|1373|[Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1373.java) | |Hard||DP, BST
1213
|1372|[Longest ZigZag Path in a Binary Tree](https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1372.java) | |Hard||DP, Tree
1314
|1371|[Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1371.java) | |Medium||String
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1374. Generate a String With Characters That Have Odd Counts
5+
*
6+
* Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.
7+
* The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.
8+
*
9+
* Example 1:
10+
* Input: n = 4
11+
* Output: "pppz"
12+
* Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
13+
*
14+
* Example 2:
15+
* Input: n = 2
16+
* Output: "xy"
17+
* Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".
18+
*
19+
* Example 3:
20+
* Input: n = 7
21+
* Output: "holasss"
22+
*
23+
* Constraints:
24+
* 1 <= n <= 500
25+
* */
26+
public class _1374 {
27+
public static class Solution1 {
28+
public String generateTheString(int n) {
29+
StringBuilder sb = new StringBuilder();
30+
if (n > 1 && n % 2 == 0) {
31+
while (n-- > 1) {
32+
sb.append("a");
33+
}
34+
} else if (n > 1) {
35+
while (n-- > 2) {
36+
sb.append("a");
37+
}
38+
sb.append("b");
39+
}
40+
sb.append("z");
41+
return sb.toString();
42+
}
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1374;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1374Test {
10+
private static _1374.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1374.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.generateTheString(1).length());
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.generateTheString(4).length());
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.generateTheString(2).length());
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(7, solution1.generateTheString(7).length());
35+
}
36+
37+
}

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