791. Custom Sort String

 1 class Solution {
 2     public String customSortString(String S, String T) {
 3         if(S.length() == 0 || T.length() == 0) return T;
 4         int[] count = new int[26];
 5         for(int i = 0; i < T.length(); i++){
 6             count[T.charAt(i) - 'a']++;
 7         }
 8         StringBuilder sb = new StringBuilder();
 9         for(int i = 0; i < S.length(); i++){
10             char c = S.charAt(i);
11             if(count[c - 'a'] != 0){
12                 for(int j = 0; j < count[c - 'a']; j++){
13                     sb.append(c);
14                 }
15             }
16             count[c - 'a'] = 0;
17         }
18         for(int i = 0; i < 26; i++){
19             if(count[i] != 0){
20                 for(int j = 0; j < count[i]; j++){
21                     sb.append((char)('a'+ i));
22                 }
23             }
24         }
25         return sb.toString();
26         
27     }
28 }

 

posted @ 2018-10-27 06:56  jasoncool1  阅读(92)  评论(0编辑  收藏  举报