Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d1f353b

Browse files
add 1400
1 parent 3bb52a9 commit d1f353b

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-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+
|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy|
1112
|1396|[Design Underground System](https://leetcode.com/problems/design-underground-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1396.java) | |Medium|Design|
1213
|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1395.java) | |Medium|Array|
1314
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java) | |Easy|Array|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1400. Construct K Palindrome Strings
8+
*
9+
* Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
10+
* Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
11+
*
12+
* Example 1:
13+
* Input: s = "annabelle", k = 2
14+
* Output: true
15+
* Explanation: You can construct two palindromes using all characters in s.
16+
* Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
17+
*
18+
* Example 2:
19+
* Input: s = "leetcode", k = 3
20+
* Output: false
21+
* Explanation: It is impossible to construct 3 palindromes using all the characters of s.
22+
*
23+
* Example 3:
24+
* Input: s = "true", k = 4
25+
* Output: true
26+
* Explanation: The only possible solution is to put each character in a separate string.
27+
*
28+
* Example 4:
29+
* Input: s = "yzyzyzyzyzyzyzy", k = 2
30+
* Output: true
31+
* Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
32+
*
33+
* Example 5:
34+
* Input: s = "cr", k = 7
35+
* Output: false
36+
* Explanation: We don't have enough characters in s to construct 7 palindromes.
37+
*
38+
* Constraints:
39+
* 1 <= s.length <= 10^5
40+
* All characters in s are lower-case English letters.
41+
* 1 <= k <= 10^5
42+
* */
43+
public class _1400 {
44+
public static class Solution1 {
45+
public boolean canConstruct(String s, int k) {
46+
if (s.length() < k) {
47+
return false;
48+
}
49+
Map<Character, Integer> map = new HashMap<>();
50+
for (char c : s.toCharArray()) {
51+
map.put(c, map.getOrDefault(c, 0) + 1);
52+
}
53+
int count = 0;
54+
for (char c : map.keySet()) {
55+
if (map.get(c) % 2 == 1) {
56+
count++;
57+
}
58+
}
59+
return count <= k;
60+
}
61+
}
62+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1400;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1400Test {
10+
private static _1400.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1400.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.canConstruct("annabelle", 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.canConstruct("leetcode", 3));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.canConstruct("true", 4));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.canConstruct("yzyzyzyzyzyzyzy", 2));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(false, solution1.canConstruct("cr", 7));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(true, solution1.canConstruct("qlkzenwmmnpkopu", 15));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(true, solution1.canConstruct("jsautfnlcmwqpzycehdulmdencthhlzsnijd", 35));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)