首页 > 其他 > 详细

LintCode Two Strings Are Anagrams

时间:2016-05-25 01:42:00      阅读:175      评论:0      收藏:0      [点我收藏+]

1. 把string变为char数组

2. 排序Arrays.sort()

public class Solution {
    /**
     * @param s: The first string
     * @param b: The second string
     * @return true or false
     */
    public boolean anagram(String s, String t) {
        if(s == null || t == null) return false;
        if(s.length() != t.length()) return false;
        
        char[] arrs = s.toCharArray();
        char[] arrt = t.toCharArray();
        Arrays.sort(arrs);
        Arrays.sort(arrt);
        
        boolean flag = true;
        for(int i = 0; i < arrs.length; i++){
            if(arrs[i] != arrt[i]){
                flag = false;
                
            }
        }
        return flag;// write your code here
    }
};

 

LintCode Two Strings Are Anagrams

原文:http://www.cnblogs.com/LittleAlex/p/5525612.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!