-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a36efe1
commit 1cbd689
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/algorithms/implementations/BeautifulNumberValidatorImpl.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,37 @@ | ||
package algorithms.implementations; | ||
|
||
import algorithms.interfaces.BeautifulNumberValidator; | ||
|
||
/** | ||
* https://www.hackerrank.com/challenges/separate-the-numbers/problem | ||
*/ | ||
|
||
public class BeautifulNumberValidatorImpl implements BeautifulNumberValidator { | ||
@Override | ||
public boolean isValidBeautifulNumber(final String s) { | ||
if (s.length() < 2) { | ||
return false; | ||
} | ||
|
||
for (int substringSize = 1; substringSize <= s.length() / 2; substringSize++) { | ||
long value = Long.parseLong(s.substring(0, substringSize)); | ||
|
||
if (value == 0) { | ||
break; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
sb.append(value); | ||
|
||
while (sb.length() < s.length() && sb.toString().equals(s.substring(0, sb.length()))) { | ||
sb.append(++value); | ||
} | ||
|
||
if (sb.toString().equals(s)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/algorithms/interfaces/BeautifulNumberValidator.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,5 @@ | ||
package algorithms.interfaces; | ||
|
||
public interface BeautifulNumberValidator { | ||
boolean isValidBeautifulNumber(final String s); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/algorithms/implementations/BeautifulNumberValidatorImplTest.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,30 @@ | ||
package algorithms.implementations; | ||
|
||
import algorithms.interfaces.BeautifulNumberValidator; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class BeautifulNumberValidatorImplTest { | ||
final BeautifulNumberValidator validator = new BeautifulNumberValidatorImpl(); | ||
|
||
@Test | ||
public void itShouldBeAValidBeautifulNumber() { | ||
assertTrue(validator.isValidBeautifulNumber("1234")); | ||
} | ||
|
||
@Test | ||
public void itShouldNotBeAValidBeautifulNumber() { | ||
assertFalse(validator.isValidBeautifulNumber("101103")); | ||
} | ||
|
||
@Test | ||
public void itShouldNotBeAValidBeautifulNumberSinceItStartsWithZero() { | ||
assertFalse(validator.isValidBeautifulNumber("010203")); | ||
} | ||
|
||
@Test | ||
public void itShouldNotBeAValidBeautifulNumberSinceItIsTooSmall() { | ||
assertFalse(validator.isValidBeautifulNumber("1")); | ||
} | ||
} |