---
# System prepended metadata

title: 0791. Custom Sort String
tags: [Leetcode, FaceBook, Medium]

---

# 0791. Custom Sort String
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/custom-sort-string/
## 思路
一开始想用hashmap记录每个字母出现的次数，但其实不需要，因为一共只有26个字母
## Code 
```java=
class Solution {
    public String customSortString(String order, String s) {
        int[] cnt = new int[26];
        
        for(char c:s.toCharArray()){
            cnt[c-'a']++;
        }
        
        StringBuilder sb = new StringBuilder();
        for(char c:order.toCharArray()){
            for(int i = 0;i < cnt[c-'a'];i++){
                sb.append(c);
            }
            cnt[c-'a'] = 0;
        }
        
        for(int i = 0;i < 26;i++){
            for(int j = 0;j < cnt[i];j++){
                sb.append((char)(i+'a'));
            }
        }
        return sb.toString();
    }
}
```