LintCode Two Strings Are Anagrams

时间:2023-03-09 15:43:47
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
}
};