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.
题意:给的两个字符串判断是否是相同字符组成的不同字符串
public class Solution { public boolean isAnagram(String s, String t) { int[] ret=new int[26]; for(int i=0;i<s.length();i++){ ret[s.charAt(i)-‘a‘]--; } for(int i=0;i<t.length();i++){ ++ret[t.charAt(i)-‘a‘]; } for(int i=0;i<26;i++){ if(ret[i]!=0) return false; } return true; } }
PS:参见之前的那个勒索信的那个,直接hash求出每个字符的数量即可
Leetcode242 Valid Anagram JAVA语言
原文:http://fulin0532.blog.51cto.com/6233825/1890860