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.
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
public class Solution { public boolean isAnagram(String s, String t) { int[] tem = new int[26]; for (int i = 0; i < s.length(); i++) { tem[s.charAt(i) - 'a']++; } for (int i = 0; i < t.length(); i++) { tem[t.charAt(i) - 'a']--; } for (int str : tem) { if (str != 0) { return false; } } return true; } }
没有评论:
发表评论