## Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.

## Solution

I explain my solution with following examples:

**Example 1234**

I start with a beginning of a string, which would be one digit number –

`1`

and try to examine a rest of the given string `234`

.Next, I check the greater number than `1`

in the rest of the string `234`

.

The next number to examine is `2`

which should follow.

It is at the beginning of the rest `234`

. The next rest is `34`

.

The next number should be `3`

. It is at the beginning of the rest.

Then the next rest is `4`

. And the next number should be `4`

because it is an increment of previous number `3`

.

The number and the rest are the same. The number `1234`

is *beautiful*

**Example 101103**

I start with a beginning of a string, which would be one digit number –

`1`

and try to examine a rest of the given string `01103`

.The next number should be

`2`

. But the rest of the string does not start with `2`

.I need to examine two digit numbers.

If I start with `10`

, the rest of the string is `1103`

.

The next number should be `11`

and it is at the beginning, my next rest is `03`

.

The next number should be `12`

but it is not equal to the rest `03`

Last, I try with `101`

and the rest is `103`

.

The next should be `102`

, but is is not. That means that `101103`

is not *beautiful*.

* Javascript solution does not work for a number 9007199254740992*.

I created solution in:

All solutions are also available on my GitHub profile.

## Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.*; public class Solution { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); t: for(int i = 0; i < tests; i++) { String s = stdin.nextLine(); for(int j = 1; j <= s.length() / 2; j++) { String head = s.substring(0, j); long headVal = Long.parseLong(head); long next = headVal + 1; String rest = s.substring(j); if(isContinous(rest, next)) { System.out.println("YES " + head); continue t; } } System.out.println("NO"); } stdin.close(); } private static boolean isContinous(String rest, long next) { String nextS = String.valueOf(next); int i = nextS.length(); while(i <= rest.length()) { if(!rest.startsWith(nextS)) { return false; } else { next = next + 1; rest = rest.substring(i); nextS = String.valueOf(next); i = nextS.length(); } } if(!rest.isEmpty()) { return false; } return true; } } |

## Javascript

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
'use strict'; const isContinous = (rest, next) => { let nextS = next; let i = nextS.length; while(i <= rest.length) { if(!rest.startsWith(nextS)) { return false; } else { next = parseInt(next) + 1; rest = rest.substring(i); nextS = next.toString(); i = nextS.length; } } if(rest.length !== 0) { return false; } return true; } const processData = input => { const lines = input.split('\n'); const max = Number.MAX_SAFE_INTEGER; t: for(let i = 1; i < lines.length; i++) { const s = lines[i]; for(let j = 1; j <= parseInt(s.length / 2); j++) { const head = s.substring(0, j); const headVal = parseInt(head); let next; if (headVal > max) { let lastChar = parseInt(head.charAt(head.length - 1)) + 1; next = head.substring(0, j - 1) + lastChar; } else { next = (headVal + 1).toString(); } const rest = s.substring(j); if(isContinous(rest, next)) { console.log("YES " + head); continue t; } } console.log("NO"); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |