LeetCode 242 有效的字母异位词

LeetCode 242 有效的字母异位词

问题描述
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

哈希表

执行用时:4 ms, 在所有 Java 提交中击败了64.92%的用户
内存消耗:38.9 MB, 在所有 Java 提交中击败了73.89%的用户

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] table = new int[26];
        for (int i = 0; i < s.length(); i++) {
            table[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            table[t.charAt(i) - 'a']--;
            if (table[t.charAt(i) - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}
原文地址:https://www.cnblogs.com/CodeSPA/p/13700509.html