package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**
 * 791. Custom Sort String

 S and T are strings composed of lowercase letters. In S, no letter occurs more than once.

 S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.

 Return any permutation of T (as a string) that satisfies this property.

 Example :
 Input:
 S = "cba"
 T = "abcd"
 Output: "cbad"
 Explanation:
 "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
 Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.

 Note:

 S has length at most 26, and no character is repeated in S.
 T has length at most 200.
 S and T consist of lowercase letters only.

 */
public class _791 {
  public static class Solution1 {
    public String customSortString(String S, String T) {
      Map<Character, Integer> map = new HashMap<>();
      for (char c : T.toCharArray()) {
        map.put(c, map.getOrDefault(c, 0) + 1);
      }
      StringBuilder sb = new StringBuilder();
      for (char c : S.toCharArray()) {
        if (map.containsKey(c)) {
          int count = map.get(c);
          while (count-- > 0) {
            sb.append(c);
          }
          map.remove(c);
        }
      }
      for (char c : map.keySet()) {
        int count = map.get(c);
        while (count-- > 0) {
          sb.append(c);
        }
      }
      return sb.toString();
    }
  }
}

@@ -82,857 +82,8 @@
 p;%0A%0A
-/**%0A * 791. Custom Sort String%0A%0A S and T are strings composed of lowercase letters. In S, no letter occurs more than once.%0A%0A S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.%0A%0A Return any permutation of T (as a string) that satisfies this property.%0A%0A Example :%0A Input:%0A S = %22cba%22%0A T = %22abcd%22%0A Output: %22cbad%22%0A Explanation:%0A %22a%22, %22b%22, %22c%22 appear in S, so the order of %22a%22, %22b%22, %22c%22 should be %22c%22, %22b%22, and %22a%22.%0A Since %22d%22 does not appear in S, it can be at any position in T. %22dcba%22, %22cdba%22, %22cbda%22 are also valid outputs.%0A%0A Note:%0A%0A S has length at most 26, and no character is repeated in S.%0A T has length at most 200.%0A S and T consist of lowercase letters only.%0A%0A */%0A
 publ
@@ -98,16 +98,18 @@
  _791 %7B%0A
+  
   public
@@ -130,24 +130,28 @@
 Solution1 %7B%0A
+    
     public S
@@ -201,16 +201,22 @@
 %7B%0A      
+  
+    
 Map%3CChar
@@ -252,32 +252,38 @@
 hMap%3C%3E();%0A      
+    
+  
 for (char c : T.
@@ -303,24 +303,32 @@
 ) %7B%0A        
+      
+  
 map.put(c, m
@@ -355,24 +355,30 @@
 ) + 1);%0A
+      
       %7D%0A
       St
@@ -365,24 +365,30 @@
           %7D%0A
+      
       String
@@ -423,24 +423,30 @@
 er();%0A      
+  
+    
 for (char c 
@@ -474,16 +474,24 @@
         
+    
+    
 if (map.
@@ -510,32 +510,42 @@
 c)) %7B%0A          
+    
+      
 int count = map.
@@ -554,32 +554,42 @@
 t(c);%0A          
+  
+        
 while (count-- %3E
@@ -598,32 +598,44 @@
 ) %7B%0A            
+        
+    
 sb.append(c);%0A  
@@ -634,34 +634,46 @@
 d(c);%0A          
-%7D%0A
+          %7D%0A  
           map.re
@@ -662,24 +662,32 @@
             
+        
 map.remove(c
@@ -685,24 +685,32 @@
 .remove(c);%0A
+        
         %7D%0A  
@@ -709,26 +709,38 @@
     %7D%0A      
-%7D%0A
+      %7D%0A      
       for (c
@@ -763,32 +763,40 @@
 et()) %7B%0A        
+    
+    
 int count = map.
@@ -803,32 +803,40 @@
 get(c);%0A        
+    
+    
 while (count-- %3E
@@ -843,32 +843,42 @@
  0) %7B%0A          
+    
+      
 sb.append(c);%0A  
@@ -883,24 +883,46 @@
         
+        %7D%0A            
 %7D%0A      
 %7D%0A      
@@ -909,26 +909,24 @@
     %7D%0A      
-%7D%0A
       return
@@ -945,16 +945,22 @@
 ();%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 791