TreeviewCopyright © aleen42 all right reserved, powered by aleen42

242. 有效的字母异位词

https://leetcode-cn.com/problems/valid-anagram/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-24 17:00:00
 * @LastEditTime: 2020-08-24 17:00:38
 * @Description: https://leetcode-cn.com/problems/valid-anagram/
 * @FilePath: \leetcode-googtech\#242. Valid Anagram\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {
    // 使用数组替代哈希表
    public boolean isAnagram(String s, String t) {
        // 判断两字符串的长度是否相同
        if(s.length() != t.length()) return false;
        // 初始化整型数组
        int[] arr = new int[26];
        // 遍历字符串,其中 s.charAt(i) - a 作为元素索引,
        // 其取值范围为 0 到 25,并将该字母出现的次数作为索引值
        for(int i = 0; i < s.length(); i++) {
            arr[s.charAt(i) - 'a'] ++;
            arr[t.charAt(i) - 'a'] --;
        }
        // 遍历数组,若元素为不为零则证明二者并非字母异位词
        for(int i = 0; i < 26; i++) {
            if(arr[i] != 0) {
                return false;
            }
        }
        return true;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-24 17:00:05
LastEditTime: 2020-08-24 17:00:27
Description: https://leetcode-cn.com/problems/valid-anagram/
FilePath: \leetcode-googtech\#242. Valid Anagram\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        # 判断两字符串的长度是否相同
        if len(s) != len(t): return False
        # 将 s 字符串转换为 set 集合(无重复性质),然后进行字符遍历
        for ch in set(s):
            # 判断当前字符在字符串 s 中出现的次数与 t 是否相等
            if s.count(ch) != t.count(ch):
                return False
        return True
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""