image_2021-11-09_15-12-58.png
52.4 KB
#medium
#N791. Custom Sort String
problem link
#solution
#N791. Custom Sort String
problem link
#solution
class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
char[] arr1=order.toCharArray();
char[] arr2=s.toCharArray();
for(int i=0; i<order.length(); i++){
for(int j=0; j<s.length(); j++){
if(arr1[i]==arr2[j]){
res.append(""+arr1[i]);
arr2[j]=' ';
}
}
}
for(int i=0; i<arr2.length; i++){
if(arr2[i]!=' ')
res.append(""+arr2[i]);
}
return res.toString();
}
}
p.s. runtime is O(m•n). the worst ever