zoukankan      html  css  js  c++  java
  • 0242. Valid Anagram (E)

    Valid Anagram (E)

    题目

    Given two strings s and t , write a function to determine if t is an anagram of s.

    Example 1:

    Input: s = "anagram", t = "nagaram"
    Output: true
    

    Example 2:

    Input: s = "rat", t = "car"
    Output: false
    

    Note:
    You may assume the string contains only lowercase alphabets.

    Follow up:
    What if the inputs contain unicode characters? How would you adapt your solution to such case?


    题意

    判断两个给定的单词(只包含小写字母)是否互为anagram,即字母组成相同但排列顺序不同的单词。

    思路

    如果只包含小写字母,只需要遍历每一个字符串,统计每个字符串中每个字母出现的次数即可。

    也可以将字符串排序后逐个比较。

    Follow up仍可以用Hash的思想进行处理,只不过不能只开一个简单的数组来当hash表。


    代码实现

    Java

    Hash

    class Solution {
        public boolean isAnagram(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
    
            int count[] = new int[26];
    
            for (int i = 0; i < s.length(); i++) {
                count[s.charAt(i) - 'a']++;
                count[t.charAt(i) - 'a']--;
            }
    
            for (int i = 0; i < 26; i++) {
                if (count[i] != 0) {
                    return false;
                }
            }
            
            return true;
        }
    }
    

    排序

    class Solution {
        public boolean isAnagram(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
    
            char[] ss = s.toCharArray();
            char[] tt = t.toCharArray();
            Arrays.sort(ss);
            Arrays.sort(tt);
            
            for (int i = 0; i < ss.length; i++) {
                if (ss[i] != tt[i]) {
                    return false;
                }
            }
            
            return true;
        }
    }
    

    Follow up

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

    JavaScript

    /**
     * @param {string} s
     * @param {string} t
     * @return {boolean}
     */
    var isAnagram = function (s, t) {
      if (s.length !== t.length) return false
      const cnt = new Map()
      for (const c of s) {
        if (!cnt.has(c)) cnt.set(c, 1)
        else cnt.set(c, cnt.get(c) + 1)
      }
      for (const c of t) {
        if (cnt.has(c) && cnt.get(c) > 0) cnt.set(c, cnt.get(c) - 1)
        else return false
      }
      return true
    }
    
  • 相关阅读:
    HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)
    HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
    HDU 5120 Intersection(2014北京赛区现场赛I题 计算几何)
    HDU 4793 Collision(2013长沙区域赛现场赛C题)
    HDU 4791 Alice's Print Service(2013长沙区域赛现场赛A题)
    HDU 4803 Poor Warehouse Keeper
    HDU HDU1558 Segment set(并查集+判断线段相交)
    HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)
    HDU 1392 Surround the Trees(凸包*计算几何)
    HDU 1174 爆头(计算几何)
  • 原文地址:https://www.cnblogs.com/mapoos/p/14398216.html
Copyright © 2011-2022 走看看