哈希——242.有效的字母异位词

1 题目描述

  1. 有效的字母异位词
    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

2 题目示例

示例 1:

输入: s = “anagram”, t = “nagaram”
输出: true

示例 2:

输入: s = “rat”, t = “car”
输出: false

3 题目提示

  • 1 <= s.length, t.length <= 5 * 104
  • s 和 t 仅包含小写字母

4 思路

方法一:排序
t 是 s 的异位词等价于「两个字符串排序后相等」。因此我们可以对字符串 s 和 t 分别排序,看排序后的字符串是否相等即可判断。此外,如果 s 和 t 的长度不同,t 必然不是 s 的异位词。

复杂度分析

时间复杂度: O(nlogn),其中 n 为 s 的长度。排序的时间复杂度为 O(nlogn),比较两个字符串是否相等时间复杂度为O(n),因此总体时间复杂度为 O(nlogn+n)=O(nlogn)。

空间复杂度:O(logn)。排序需要 O(logn) 的空间复杂度。

方法二:哈希表
从另一个角度考虑,t 是 s 的异位词等价于「两个字符串中字符出现的种类和次数均相等」。由于字符串只包含 26 个小写字母,因此我们可以维护一个长度为 26 的频次数组 table,先遍历记录字符串 s 中字符出现的频次,然后遍历字符串 t,减去 table 中对应的频次,如果出现 table[i]<0,则说明 t 包含一个不在 s 中的额外字符,返回 false 即可。

复杂度分析

时间复杂度:O(n),其中 n 为 s 的长度。

空间复杂度:O(S),其中 S 为字符集大小,此处 S=26。

5 我的答案

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;
    }
}

class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        if (s.length() != t.length()) {
    
    
            return false;
        }
        Map<Character, Integer> table = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
    
    
            char ch = s.charAt(i);
            table.put(ch, table.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < t.length(); i++) {
    
    
            char ch = t.charAt(i);
            table.put(ch, table.getOrDefault(ch, 0) - 1);
            if (table.get(ch) < 0) {
    
    
                return false;
            }
        }
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44688973/article/details/125955996