242. Valid Anagram

来源:互联网 发布:农村淘宝服务点查询 编辑:程序博客网 时间:2024/05/17 04:05

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[] map = new int[26];        char[] cs = s.toCharArray();        for(char c : cs) {            int x = c - 'a';            map[x] += 1;        }        cs = t.toCharArray();        for(char c : cs) {            int x = c - 'a';            if(map[x] == 0) {                return false;            }            map[x] -= 1;        }        for(int x : map) {            if(x != 0) {                return false;            }        }        return true;    }}//是否字母顺序不一样,用一个哈希表记录其中一个字符串的每一个字符及出现的次数。用另一个字符串的字符当作key获取value判断是否存在。
0 0
原创粉丝点击