Skip to content

Commit 63f2085

Browse files
committed
Separated Number Task solution
1 parent f479958 commit 63f2085

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package hackerRank_JavaProblemSolving;
2+
3+
import java.util.*;
4+
5+
public class Problem104_SeparateNumbers {
6+
7+
static void separateNumbers(String s) {
8+
int s_len = s.length();
9+
String subStrFirstIndex ="";
10+
int sub_s_size = 1;
11+
long aValue = -1;
12+
while ( sub_s_size <= (s_len/2)) {
13+
subStrFirstIndex = s.substring(0,sub_s_size);
14+
aValue=Long.valueOf(subStrFirstIndex);
15+
long nextValueInt = aValue+1;
16+
int indexIn = sub_s_size;
17+
int len_next_val = -1;
18+
while ( indexIn < s_len ) {
19+
String aNextValueStr = String.valueOf(nextValueInt);
20+
len_next_val = aNextValueStr.length();
21+
String aCutNextValueStr = "";
22+
if ( indexIn+len_next_val < s_len) { // we can run out of index
23+
aCutNextValueStr = s.substring(indexIn,indexIn+len_next_val);
24+
} else {
25+
aCutNextValueStr = s.substring(indexIn,s_len);
26+
}
27+
if ( !aNextValueStr.equals(aCutNextValueStr) ) {
28+
break;
29+
} else { // next iteration
30+
nextValueInt++;
31+
}
32+
indexIn = indexIn+len_next_val;
33+
}
34+
if ( indexIn == s_len) {
35+
System.out.println("YES "+aValue);
36+
return;
37+
}
38+
sub_s_size++;
39+
}
40+
System.out.println("NO");
41+
}
42+
43+
private static final Scanner scanner = new Scanner(System.in);
44+
45+
public static void main(String[] args) {
46+
int q = scanner.nextInt();
47+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
48+
49+
for (int qItr = 0; qItr < q; qItr++) {
50+
String s = scanner.nextLine();
51+
52+
separateNumbers(s);
53+
}
54+
55+
scanner.close();
56+
// Test case 20
57+
// 429496729542949672964294967297
58+
// YES 4294967295
59+
// 429496729542949672964294967296
60+
// NO
61+
// 429496729542949672964294967287
62+
// NO
63+
// 429496729542949672964294967197
64+
// NO
65+
// 42949672954294967296429496729
66+
// NO
67+
// 4294967295429496729642949672
68+
// NO
69+
// 429496729500000000000000000001
70+
// NO
71+
// 42949672950123456789
72+
// NO
73+
// 4294967295000010020030000456789
74+
// NO
75+
// 4294967295000102003004005
76+
// NO
77+
}
78+
}

0 commit comments

Comments
 (0)