fork download
  1. def solve(String s) {
  2. int zeros = 0
  3. int nines = 0
  4. int takeOneFromPos = s.length() - 1
  5. while (s[takeOneFromPos] == '0' && takeOneFromPos >= 0) {
  6. --takeOneFromPos
  7. ++zeros
  8. }
  9. if (takeOneFromPos == -1) return '-1'
  10. int addOneToPos = takeOneFromPos - 1
  11. while (s[addOneToPos] == '9' && addOneToPos >= 0) {
  12. --addOneToPos
  13. ++nines
  14. }
  15. StringBuilder result = new StringBuilder()
  16. if (addOneToPos == -1) {
  17. result.append(1)
  18. }
  19. else {
  20. result.append(s.substring(0, addOneToPos))
  21. result.append(s[addOneToPos].toInteger() + 1)
  22. }
  23. result.append('0' * zeros)
  24. result.append(s[takeOneFromPos].toInteger() - 1)
  25. result.append('9' * nines)
  26. return result.toString()
  27. }
  28.  
  29. assert solve('113') == '122'
  30. assert solve('0050') == '0104'
  31. assert solve('103') == '112'
  32. assert solve('190') == '208'
  33. assert solve('99100') == '100099'
  34.  
Success #stdin #stdout 1.35s 332992KB
stdin
Standard input is empty
stdout
Standard output is empty