-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustom_Sort_String.java
More file actions
41 lines (41 loc) · 1.24 KB
/
Custom_Sort_String.java
File metadata and controls
41 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
import java.io.*;
import java.lang.*;
public class Custom_Sort_String {
class Solution {
public String customSortString(String order, String s) {
HashMap<Character , Integer> hm = new HashMap<>();
for(int i = 0 ; i<s.length() ; i++)
{
hm.put(s.charAt(i) , hm.getOrDefault(s.charAt(i) , 0) + 1);
}
String ans = "";
for(int i = 0 ; i<order.length() ; i++)
{
if(hm.containsKey(order.charAt(i)))
{
int x = hm.get(order.charAt(i));
while(x>0)
{
ans = ans + order.charAt(i);
x--;
}
hm.remove(order.charAt(i));
}
}
Iterator hmi = hm.entrySet().iterator();
while(hmi.hasNext())
{
Map.Entry map = (Map.Entry)hmi.next();
char c = (char)map.getKey();
int m = (int)map.getValue();
while(m>0)
{
ans = ans + c;
m--;
}
}
return ans;
}
}
}