算法题——确定两串乱序同构

时间:2015-07-30 11:25:02   收藏:0   阅读:696

欢迎转载,请附出处:
http://blog.csdn.net/as02446418/article/details/47145467

给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。
给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。
测试样例:
“This is nowcoder”,”is This nowcoder”
返回:true
“Here you are”,”Are you here”
返回:false

public class Same {
     public static boolean checkSam(String stringA, String stringB) {
            // write code here
            if(stringA.length()!=stringB.length())return false;

            Map<Character,Integer> map = new HashMap<Character,Integer>();
            for(int i=0;i<stringA.length();i++){
                char temp = stringA.charAt(i);
                if(map.containsKey(temp)){
                    map.put(temp,map.get(temp)+1);
                }else{
                    map.put(temp,1);
                }

            }

            for(int i=0;i<stringB.length();i++){
                char temp = stringB.charAt(i);
                if(map.containsKey(temp)){
                if(map.get(temp)>0){
                    map.put(temp,map.get(temp)-1);              
                    }else{
                        return false;
                    }
                }
                    else return false;
            }       

            return true;
        }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/as02446418/article/details/47145467

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!