package com.fishercoder.solutions;

import java.util.TreeSet;

/**
 * 821. Shortest Distance to a Character

  Given a string S and a character C,
  return an array of integers representing the shortest distance from the character C in the string.

 Example 1:

 Input: S = "loveleetcode", C = 'e'
 Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

 Note:

 S string length is in [1, 10000].
 C is a single character, and guaranteed to be in string S.
 All letters in S and C are lowercase.

 */
public class _821 {

  public static class Solution1 {
    public int[] shortestToChar(String S, char C) {
      int[] result = new int[S.length()];
      TreeSet<Integer> cIndices = new TreeSet();
      for (int i = 0; i < S.length(); i++) {
        if (S.charAt(i) == C) {
          cIndices.add(i);
        }
      }
      for (int i = 0; i < S.length(); i++) {
        int leftDist = Integer.MAX_VALUE;
        if (cIndices.floor(i) != null) {
          leftDist = Math.abs(cIndices.floor(i) - i);
        }
        int rightDist = Integer.MAX_VALUE;
        if (cIndices.ceiling(i) != null) {
          rightDist = Math.abs(cIndices.ceiling(i) - i);
        }
        result[i] = Math.min(leftDist, rightDist);
      }
      return result;
    }
  }
}

@@ -60,437 +60,8 @@
 t;%0A%0A
-/**%0A * 821. Shortest Distance to a Character%0A%0A  Given a string S and a character C,%0A  return an array of integers representing the shortest distance from the character C in the string.%0A%0A Example 1:%0A%0A Input: S = %22loveleetcode%22, C = 'e'%0A Output: %5B3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0%5D%0A%0A Note:%0A%0A S string length is in %5B1, 10000%5D.%0A C is a single character, and guaranteed to be in string S.%0A All letters in S and C are lowercase.%0A%0A */%0A
 publ
@@ -79,16 +79,18 @@
 21 %7B%0A%0A  
+ 
+ 
 public s
@@ -113,16 +113,20 @@
 tion1 %7B%0A
+    
     publ
@@ -175,16 +175,22 @@
 %7B%0A      
+    
+  
 int%5B%5D re
@@ -217,16 +217,22 @@
 gth()%5D;%0A
+      
       Tr
@@ -274,24 +274,30 @@
 et();%0A      
+  
+    
 for (int i =
@@ -327,24 +327,32 @@
 ) %7B%0A        
+     
+   
 if (S.charAt
@@ -373,16 +373,26 @@
         
+      
+    
 cIndices
@@ -412,18 +412,38 @@
     
+     
-%7D%0A
+   %7D%0A     
       
+ 
 %7D%0A
+      
     
@@ -487,24 +487,32 @@
 ) %7B%0A        
+   
+     
 int leftDist
@@ -533,32 +533,40 @@
 _VALUE;%0A        
+    
+    
 if (cIndices.flo
@@ -592,16 +592,26 @@
         
+     
+     
 leftDist
@@ -654,26 +654,40 @@
         
-%7D%0A
+        %7D%0A      
         
 int righ
@@ -674,24 +674,26 @@
             
+  
 int rightDis
@@ -715,32 +715,40 @@
 _VALUE;%0A        
+       
+ 
 if (cIndices.cei
@@ -776,16 +776,26 @@
         
+        
+  
 rightDis
@@ -833,34 +833,50 @@
 ) - i);%0A        
-%7D%0A
+        %7D%0A        
         result%5Bi
@@ -912,26 +912,38 @@
 ist);%0A      
-%7D%0A
+      %7D%0A      
       return
@@ -955,16 +955,22 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 821