File tree 3 files changed +51
-1
lines changed
3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 23
23
| 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 ) |
24
24
| 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 ) |
25
25
| 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 )] ( ) |
27
27
| 2 | [ ] ( ) | Easy | |
28
28
| 2 | [ ] ( ) | Easy | |
29
29
| 2 | [ ] ( ) | Easy | |
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<module type =" JAVA_MODULE" version =" 4" >
3
3
<component name =" NewModuleRootManager" >
4
+ <output url =" file:///out/leetcode-algorithms" />
5
+ <output-test url =" file:///test/leetcode-algorithms" />
4
6
<content url =" file://$MODULE_DIR$" >
5
7
<sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
6
8
<excludeFolder url =" file://$MODULE_DIR$/venv" />
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments