Skip to content

Commit d5d67b5

Browse files
authored
Merge pull request #3290 from a1exanddrovich/1849-dev
Create: 1849-splitting-a-string-into-descending-consecutive-values.java
2 parents c142b02 + d841e41 commit d5d67b5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.math.BigInteger;
2+
3+
class Solution {
4+
public boolean splitString(String s) {
5+
for (int i = 0; i < s.length() - 1; i++) {
6+
BigInteger curValue = new BigInteger(s.substring(0, i + 1));
7+
if (backtrack(i + 1, s, curValue)) {
8+
return true;
9+
}
10+
}
11+
12+
return false;
13+
}
14+
15+
private boolean backtrack(int startIndex,
16+
String s,
17+
BigInteger lastValue) {
18+
if (startIndex >= s.length()) {
19+
return true;
20+
}
21+
22+
for (int i = startIndex; i < s.length(); i++) {
23+
BigInteger curValue = new BigInteger(s.substring(startIndex, i + 1));
24+
if (lastValue.subtract(curValue).compareTo(BigInteger.ONE) == 0 &&
25+
backtrack(i + 1, s, curValue)) {
26+
return true;
27+
}
28+
}
29+
30+
return false;
31+
}
32+
}

0 commit comments

Comments
 (0)