Separate the Numbers

  • + 0 comments

    instead of a lot of comparisons, just take first number and build it and then check

        public static void separateNumbers(String s) {
            if (s.charAt(0) == '0') {
                System.out.println("NO");
                return;
            } else {
                for (int i = 1; i < (s.length() / 2)+1; i++) {
                    Long num = Long.valueOf(s.substring(0, i));
                    StringBuilder b = new StringBuilder(s.substring(0,i));
                    Long l = Long.valueOf(num);
                    while (b.length() < s.length()) {
                        b.append(l + 1);
                        l = l + 1;
                    }
                    if (b.toString().equals(s)) {
                        System.out.println("YES " + num);
                        return;
                    }
                }
                System.out.println("NO");
            }
        }