Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.math.BigInteger;

class Solution {
public boolean splitString(String s) {
for (int i = 0; i < s.length() - 1; i++) {
BigInteger curValue = new BigInteger(s.substring(0, i + 1));
if (backtrack(i + 1, s, curValue)) {
return true;
}
}

return false;
}

private boolean backtrack(int startIndex,
String s,
BigInteger lastValue) {
if (startIndex >= s.length()) {
return true;
}

for (int i = startIndex; i < s.length(); i++) {
BigInteger curValue = new BigInteger(s.substring(startIndex, i + 1));
if (lastValue.subtract(curValue).compareTo(BigInteger.ONE) == 0 &&
backtrack(i + 1, s, curValue)) {
return true;
}
}

return false;
}
}