其他分享
首页 > 其他分享> > 面试题 01.02. 判定是否互为字符重排

面试题 01.02. 判定是否互为字符重排

作者:互联网

public boolean CheckPermutation(String s1, String s2) {
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        int n = c1.length;
        if(n!=c2.length) return false;
        Map<Character,Integer> map1 = new HashMap<>();
        Map<Character,Integer> map2 = new HashMap<>();
        for(int i = 0;i<n;i++){
            if(!map1.containsKey(c1[i])){
                map1.put(c1[i],1);
            }else{
                map1.put(c1[i],map1.get(c1[i])+1);
            }
            if(!map2.containsKey(c2[i])){
                map2.put(c2[i],1);
            }else{
                map2.put(c2[i],map2.get(c2[i])+1);
            }
        }
        for(char c : c1){
            if(!map2.containsKey(c)) return false;
            if(!map1.get(c).equals(map2.get(c))) return false;
        }
        return true;
    }

 

标签:Map,面试题,HashMap,int,s2,char,01.02,重排,String
来源: https://www.cnblogs.com/taoyuxin/p/13544857.html