题目链接:https://leetcode.com/problems/valid-anagram/
题目: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.
Note:
You may assume the string contains only lowercase alphabets.
解题思路:分别统计s和t的每个字母的个数,如果均相同则为Anagram。
示例代码:
- package com.test.demo;
-
-
-
-
- public class Solution
- {
- public boolean isAnagram(String s, String t)
- {
- int[] s_num=fun(s);
- int[] t_num=fun(t);
- for(int i=0;i<s_num.length;i++)
- {
- if(s_num[i]!=t_num[i])
- {
- return false;
- }
- }
- return true;
- }
-
-
-
-
-
- private int[] fun(String str)
- {
- int num[]=new int[26];
- for(int i=0;i<str.length();i++)
- {
- int k = Integer.valueOf(str.charAt(i)).intValue()-97;
- num[k]++;
- }
- return num;
- }
- }