Skip to content

Commit

Permalink
Merge pull request #7 from jaebradley/fix-separate-the-numbers-implem…
Browse files Browse the repository at this point in the history
…entation

update implementation
  • Loading branch information
jaebradley committed Jul 20, 2017
2 parents a48891a + 5b74606 commit f2e8605
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@

/**
* https://www.hackerrank.com/challenges/separate-the-numbers/problem
* https://codereview.stackexchange.com/questions/169679/beautiful-number-validator/169705#169705
*/

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;
public boolean isValidBeautifulNumber(String s, long prefix) {
for (int i = 0; i < s.length(); ) {
String number = String.valueOf(prefix);
if (!s.substring(i, i + number.length()).equals(number)) {
return false;
}

StringBuilder sb = new StringBuilder();
sb.append(value);
i += number.length();
prefix++;
}

while (sb.length() < s.length() && sb.toString().equals(s.substring(0, sb.length()))) {
sb.append(++value);
}
return true;
}

if (sb.toString().equals(s)) {
public boolean isValidBeautifulNumber(String s) {
for (int substringSize = 1; substringSize <= s.length() / 2; substringSize++) {
if (isValidBeautifulNumber(s, Long.parseLong(s.substring(0, substringSize)))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public interface BeautifulNumberValidator {
boolean isValidBeautifulNumber(final String s);
boolean isValidBeautifulNumber(final String s, final long prefix);
}

0 comments on commit f2e8605

Please sign in to comment.