package leetcode;

/**
 * https://leetcode.com/problems/decoded-string-at-index/
 */
public class Problem880 {
    public String decodeAtIndex(String S, int K) {
        String answer = "";
        int size = 0;
        outer:
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) >= 'a' && S.charAt(i) <= 'z') {
                answer += S.charAt(i);
                size++;
            } else {
                size = 0;
                int repeat = S.charAt(i) - '0';
                String tmp = "";
                for (int j = 0; j < repeat; j++) {
                    tmp += answer;
                    size += answer.length();
                    if (size > K) {
                        answer = tmp;
                        break outer;
                    }
                }
                answer = tmp;
            }
            if (size > K) {
                break;
            }
        }
        return "" + answer.charAt(K - 1);
    }

    public static void main(String[] args) {
        Problem880 prob = new Problem880();
//        System.out.println(prob.decodeAtIndex("leet2code3", 10)); // o
//        System.out.println(prob.decodeAtIndex("ha22", 5)); // h
//        System.out.println(prob.decodeAtIndex("hello23", 10)); // o
//        System.out.println(prob.decodeAtIndex("a2345678999999999999999", 1)); // a
        System.out.println(prob.decodeAtIndex("a2345678999999999999999", 1000000000)); // a
    }
}

@@ -167,64 +167,22 @@
     
-String answer = %22%22;%0A        int size = 0;%0A        outer:
+long size = 0;
 %0A   
@@ -289,47 +289,8 @@
 ) %7B%0A
-                answer += S.charAt(i);%0A
     
@@ -355,40 +355,9 @@
 ize 
-= 0;%0A                int repeat 
+*
 = S.
@@ -389,28 +389,9 @@
     
-    String tmp = %22%22;
+%7D
 %0A   
@@ -399,310 +399,213 @@
     
- 
+%7D%0A
        
-for (
+ 
 int 
-j = 0; j %3C repeat; j++) %7B%0A                    tmp += answer;%0A                    size += answer.length();%0A                    if (size %3E K) %7B%0A                        answer = tmp;%0A                        break outer;%0A                    %7D%0A                %7D%0A                answer = tmp
+k = K;%0A        for (int i = S.length() - 1; i %3E= 0; i--) %7B%0A            k %25= size;%0A            if (k == 0 && S.charAt(i) %3E= 'a' && S.charAt(i) %3C= 'z') %7B%0A                return %22%22 + S.charAt(i)
 ;%0A  
@@ -608,32 +608,33 @@
 ;%0A            %7D%0A
+%0A
             if (
@@ -637,56 +637,51 @@
 if (
-size %3E K) %7B%0A                break;%0A            %7D
+S.charAt(i) %3E= 'a' && S.charAt(i) %3C= 'z') %7B
 %0A   
@@ -685,26 +685,24 @@
         
-%7D%0A
         
 return %22
@@ -697,523 +697,122 @@
     
-return %22%22 + answer.charAt(K - 1)
+size--
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem880 prob = new Problem880();%0A//        System.out.println(prob.decodeAtIndex(%22leet2code3%22, 10)); // o%0A//        System.out.println(prob.decodeAtIndex(%22ha22%22, 5)); // h%0A//        System.out.println(prob.decodeAtIndex(%22hello23%22, 10)); // o%0A//        System.out.println(prob.decodeAtIndex(%22a2345678999999999999999%22, 1)); // a%0A        System.out.println(prob.decodeAtIndex(%22a2345678999999999999999%22, 1000000000)); // a
+    %7D else %7B%0A                size /= S.charAt(i) - '0';%0A            %7D%0A        %7D%0A        return %22%22;
 %0A   

Solve problem 880