Skip to content

Commit f11a653

Browse files
solves count and say
1 parent 1173aef commit f11a653

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RemoveElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/python/remove_element.py)|
2424
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/NeedleInHaystack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/python/needle_in_haystack.py)|
2525
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)]() |
26-
| 2 | []() | Easy | |
26+
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)]() |
2727
| 2 | []() | Easy | |
2828
| 2 | []() | Easy | |
2929
| 2 | []() | Easy | |

leetcode-algorithms.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="JAVA_MODULE" version="4">
33
<component name="NewModuleRootManager">
4+
<output url="file:///out/leetcode-algorithms" />
5+
<output-test url="file:///test/leetcode-algorithms" />
46
<content url="file://$MODULE_DIR$">
57
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
68
<excludeFolder url="file://$MODULE_DIR$/venv" />

src/CountAndSay.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Scanner;
4+
5+
public class CountAndSay {
6+
private static String countAndSay(int number) {
7+
if (number == 1) {
8+
return "1";
9+
}
10+
11+
String previous = countAndSay(number - 1);
12+
List<Pair> pairs = getFrequencies(previous);
13+
StringBuilder result = new StringBuilder();
14+
for (Pair pair : pairs) {
15+
result.append(pair.frequency).append(pair.character);
16+
}
17+
return result.toString();
18+
}
19+
20+
private static List<Pair> getFrequencies(String string) {
21+
char current = string.charAt(0);
22+
int frequency = 0;
23+
List<Pair> result = new ArrayList<>();
24+
25+
for (int index = 0 ; index < string.length() ; index++) {
26+
if (string.charAt(index) != current) {
27+
result.add(new Pair(current, frequency));
28+
frequency = 1;
29+
current = string.charAt(index);
30+
} else {
31+
frequency++;
32+
}
33+
}
34+
35+
result.add(new Pair(current, frequency));
36+
return result;
37+
}
38+
39+
private static class Pair {
40+
final char character;
41+
final int frequency;
42+
43+
Pair(char character, int frequency) {
44+
this.character = character;
45+
this.frequency = frequency;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)