LintCode Two Strings Are Anagrams

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LintCode Two Strings Are Anagrams相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Two Strings Are Anagrams

[LeetCode] 2068. Check Whether Two Strings are Almost Equivalent

LintCode - Merge Two Sorted List

lintcode-easy-Add two numbers

lintcode-medium-Divide Two Integers

lintcode-easy-Compare Strings