8000 add 1417 · himanshu272/Leetcode@73ba8d1 · GitHub
[go: up one dir, main page]

Skip 8000 to content

Commit 73ba8d1

Browse files
add 1417
1 parent df55ff1 commit 73ba8d1

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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+
|1419|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1419.java) | |Easy|String|
1112
|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|
1213
|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|
1314
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack|
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1417. Reformat The String
8+
*
9+
* Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).
10+
* You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit.
11+
* That is, no two adjacent characters have the same type.
12+
* Return the reformatted string or return an empty string if it is impossible to reformat the string.
13+
*
14+
* Example 1:
15+
* Input: s = "a0b1c2"
16+
* Output: "0a1b2c"
17+
* Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.
18+
*
19+
* Example 2:
20+
* Input: s = "leetcode"
21+
* Output: ""
22+
* Explanation: "leetcode" has only characters so we cannot separate them by digits.
23+
*
24+
* Example 3:
25+
* Input: s = "1229857369"
26+
* Output: ""
27+
* Explanation: "1229857369" has only digits so we cannot separate them by characters.
28+
*
29+
* Example 4:
30+
* Input: s = "covid2019"
31+
* Output: "c2o0v1i9d"
32+
*
33+
* Example 5:
34+
* Input: s = "ab123"
35+
* Output: "1a2b3"
36+
*
37+
* Constraints:
38+
* 1 <= s.length <= 500
39+
* s consists of only lowercase English letters and/or digits.
40+
* */
41+
public class _1417 {
42+
public static class Solution1 {
43+
public String reformat(String s) {
44+
List<Character> characterList = new ArrayList<>();
45+
List<Character> numberList = new ArrayList<>();
46+
for (char c : s.toCharArray()) {
47+
if (Character.isAlphabetic(c)) {
48+
characterList.add(c);
49+
} else {
50+
numberList.add(c);
51+
}
52+
}
53+
if (Math.abs(characterList.size() - numberList.size()) > 1) {
54+
return "";
55+
} else {
56+
StringBuilder sb = new StringBuilder();
57+
if (characterList.size() > numberList.size()) {
58+
for (int i = 0; i < characterList.size() - 1; i++) {
59+
sb.append(characterList.get(i));
60+
sb.append(numberList.get(i));
61+
}
62+
sb.append(characterList.get(characterList.size() - 1));
63+
} else if (characterList.size() == numberList.size()) {
64+
for (int i = 0; i < numberList.size(); i++) {
65+
sb.append(numberList.get(i));
66+
sb.append(characterList.get(i));
67+
}
68+
} else {
69+
for (int i = 0; i < numberList.size() - 1; i++) {
70+
sb.append(numberList.get(i));
71+
sb.append(characterList.get(i));
72+
}
73+
sb.append(numberList.get(numberList.size() - 1));
74+
}
75+
return sb.toString();
76+
}
77+
}
78+
}
79+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1417;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1417Test {
10+
private static _1417.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1417.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("0a1b2c", solution1.reformat("a0b1c2"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("c2o0v1i9d", solution1.reformat("covid2019"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
0