Valid Anagram

[LeetCode]

https://leetcode.com/problems/valid-anagram/

Total Accepted: 78186 Total Submissions: 187211 Difficulty: Easy

Question

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.

Ways

public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] sTimes=new int[26];
int[] tTimes=new int[26];
for(int i=0;i<s.length();i++){
sTimes[s.charAt(i)-'a']+=1;
tTimes[t.charAt(i)-'a']+=1;
}
for(int i=0;i<sTimes.length;i++){
if(sTimes[i]!=tTimes[i]){
return false;
}
}
return true;
}
}


AC:12ms

public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] times=new int[26];
for(int i=0;i<s.length();i++){
times[s.charAt(i)-'a']+=1;
times[t.charAt(i)-'a']-=1;
}
for(int i=0;i<times.length;i++){
if(times[i]!=0){
return false;
}
}
return true;
}
}


AC:10ms

public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
for(int i=0;i<s.length();i++){
if(t.indexOf(s.charAt(i))==-1){
return false;
}else{
t.replace(""+s.charAt(i),"");
}
}
if(!t.equals("")){
return false;
}
return true;
}
}


Date

2016/4/30 13:08:21

LeetCode:242 Valid Anagram Java

2015-08-05 21:07:08

LeetCode（242）Valid Anagram

2015-11-17 13:33:46

LeetCode 242. Valid Anagram 题解（C++）

2016-10-19 13:43:33

leetcode 242: Valid Anagram

2015-08-02 15:57:10

242. Valid Anagram [easy] (Python)

2016-05-14 15:07:05

【LEETCODE】242-Valid Anagram [Python]

2016-01-29 11:04:15

leetcode解题之242# Valid Anagram Java版 （判断两个字母串是否具有相同的字母组合）

2017-03-19 13:11:03

leetcode:468. Validate IP Address

2017-03-13 14:43:43

leetcode242---Valid Anagram（判断两字符串是否为变位词）

2016-01-11 11:30:13

[LeetCode261] Graph Valid Tree

2015-11-25 12:45:28