Custom Sort | Easy to Understand

Storing the value of str in Hash, and checking in order if the value of order is in HashMap-> then adding the value to the StringBuilder.

class Solution {
public String customSortString(String order, String str1) {
Map<Character,Integer> map= new HashMap<>();

    for(int i=0;i<str1.length();i++){
        if(map.containsKey(str1.charAt(i))){
            map.put(str1.charAt(i), map.get(str1.charAt(i))+1);
        }
        else{
            map.put(str1.charAt(i), 1);
        }
    }
	
    StringBuilder str= new StringBuilder();
    for(int i=0;i<order.length();i++){
        char x= order.charAt(i);
        if(map.containsKey(x)){
            int y= map.get(x);
            while(y>0){
                str.append(x);
                y--;
            }
            map.remove(x);
        }
    }
	
    for (Map.Entry<Character, Integer> entry : map.entrySet()){
        char x= entry.getKey();
        int y= entry.getValue()
        while(y>0){
            str.append(x);
            y--;
        }
    }
	
    return str.toString();
}

}

Comments (0)