-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add project euler.net TriplicateNumbers problem
- Loading branch information
1 parent
cf2ff38
commit af3fb79
Showing
7 changed files
with
299 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
java-dsa/src/main/java/io/giovannymassuia/eulernet/triplicatenumber/TriplicateNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.giovannymassuia.eulernet.triplicatenumber; | ||
|
||
/** | ||
* A triplicate number is a positive integer such that, after repeatedly removing three consecutive | ||
* identical digits from it, all its digits can be removed.</p> | ||
* | ||
* <p> | ||
* For example, the integer 122555211 is a triplicate number: | ||
* </p> | ||
* <p> | ||
* 122[555]211 -> 1[222]11 -> [111] -> . | ||
* </p> | ||
* On the other hand, neither 663633 nor 9990 are triplicate numbers. | ||
*/ | ||
public interface TriplicateNumbers { | ||
|
||
boolean isTriplicate(long number); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...sa/src/main/java/io/giovannymassuia/eulernet/triplicatenumber/TriplicateNumbersNaive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.giovannymassuia.eulernet.triplicatenumber; | ||
|
||
public class TriplicateNumbersNaive implements TriplicateNumbers { | ||
|
||
@Override | ||
public boolean isTriplicate(long number) { | ||
String numberString = String.valueOf(number); | ||
// char[] numbers = numberString.toCharArray(); | ||
// Map<Character, Integer> counter = new HashMap<>(); | ||
// | ||
// for (char n : numbers) { | ||
// counter.merge(n, 1, Integer::sum); | ||
// } | ||
|
||
int len = numberString.length() / 3; | ||
|
||
for (int i = 0; i < len; i++) { | ||
for (int j = 0; j <= 9; j++) { | ||
String x = String.valueOf(j).repeat(3); | ||
if (numberString.contains(x)) { | ||
numberString = numberString.replace(x, ""); | ||
} | ||
|
||
if (numberString.isEmpty()) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return numberString.isEmpty(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...sa/src/main/java/io/giovannymassuia/eulernet/triplicatenumber/TriplicateNumbersStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.giovannymassuia.eulernet.triplicatenumber; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
|
||
public class TriplicateNumbersStack implements TriplicateNumbers { | ||
|
||
@Override | ||
public boolean isTriplicate(long number) { | ||
// 122555244411L | ||
// [1] <-> [2] <-> [5] <-> [2] <-> [4] <-> [1] | ||
// 1 2 3 1 3 2 | ||
return solutionWithTuple(number) && solutionWithMap(number); | ||
} | ||
|
||
private boolean solutionWithMap(long number) { | ||
Stack<Character> stack = new Stack<>(); | ||
Map<Character, Integer> counter = new HashMap<>(); | ||
|
||
char[] numbers = String.valueOf(number).toCharArray(); | ||
|
||
stack.push(numbers[0]); | ||
counter.put(numbers[0], 1); | ||
|
||
for (int i = 1; i < numbers.length; i++) { | ||
char curr = numbers[i]; | ||
|
||
counter.merge(curr, 1, Integer::sum); | ||
|
||
if (!stack.isEmpty() && stack.peek() == curr) { | ||
if (counter.get(curr) == 3) { | ||
stack.pop(); | ||
counter.put(curr, 0); | ||
} | ||
} else { | ||
stack.push(curr); | ||
} | ||
} | ||
|
||
return stack.isEmpty(); | ||
} | ||
|
||
private boolean solutionWithTuple(long number) { | ||
Stack<Tuple> stack = new Stack<>(); | ||
char[] numbers = String.valueOf(number).toCharArray(); | ||
|
||
for (char n : numbers) { | ||
if (!stack.isEmpty() && stack.peek().c == n) { | ||
if (++stack.peek().count == 3) { | ||
stack.pop(); | ||
} | ||
continue; | ||
} | ||
stack.push(Tuple.of(n)); | ||
} | ||
|
||
return stack.isEmpty(); | ||
} | ||
|
||
static class Tuple { | ||
|
||
char c; | ||
int count = 1; | ||
|
||
Tuple(char c) { | ||
this.c = c; | ||
} | ||
|
||
static Tuple of(char c) { | ||
return new Tuple(c); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...dsa/src/test/java/io/giovannymassuia/eulernet/triplicatenumber/TriplicateNumbersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.giovannymassuia.eulernet.triplicatenumber; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class TriplicateNumbersTest { | ||
|
||
TriplicateNumbers triplicateNumbersNaive = new TriplicateNumbersNaive(); | ||
TriplicateNumbers triplicateNumbersStack = new TriplicateNumbersStack(); | ||
|
||
@ParameterizedTest | ||
@MethodSource("buildInputs") | ||
void isTriplicateNaive(long input, boolean expect) { | ||
assertEquals(triplicateNumbersNaive.isTriplicate(input), expect); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("buildInputs") | ||
void isTriplicateStack(long input, boolean expect) { | ||
assertEquals(triplicateNumbersStack.isTriplicate(input), expect); | ||
} | ||
|
||
private static Stream<Arguments> buildInputs() { | ||
return Stream.of( | ||
Arguments.of(122555211L, true), | ||
Arguments.of(663633L, false), | ||
Arguments.of(9990L, false), | ||
Arguments.of(122555244411L, true), | ||
Arguments.of(122255522211L, true), | ||
Arguments.of(122344433211L, true), | ||
Arguments.of(411433314111444L, false) | ||
); | ||
} | ||
} |