package leetcode;

/**
 * https://leetcode.com/problems/shortest-distance-to-a-character/
 */
public class Problem821 {
    public int[] shortestToChar(String S, char C) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem821 prob = new Problem821();
        System.out.println(prob.shortestToChar("loveleetcode", 'e')); // [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -229,242 +229,754 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem821 prob = new Problem821();%0A        System.out.println(prob.shortestToChar(%22loveleetcode%22, 'e')); // %5B3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0%5D
+List%3CInteger%3E indexes = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C S.length(); i++) %7B%0A            if (S.charAt(i) == C) %7B%0A                indexes.add(i);%0A            %7D%0A        %7D%0A        int%5B%5D result = new int%5BS.length()%5D;%0A        int idx = 0;%0A        for (int i = 0; i %3C S.length(); i++) %7B%0A            if (i %3E indexes.get(idx)) %7B%0A                idx++;%0A                if (idx == indexes.size()) %7B%0A                    idx--;%0A                %7D%0A            %7D%0A            if (idx - 1 %3E= 0) %7B%0A                result%5Bi%5D = Math.min(Math.abs(indexes.get(idx - 1) - i),%0A                    Math.abs(indexes.get(idx) - i));%0A            %7D else %7B%0A                result%5Bi%5D = Math.abs(indexes.get(idx) - i);%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 821