sort simple

Copyright: Author: xp_9512 Source: CSDN Copyright: This article is a blogger original article, reproduced, please attach Bowen link! Original: https://blog.csdn.net/qq_40981804/article/details/91413773

Ectopic effective letters

  • Title
    Given two strings s and t, write a function to determine whether the ectopic words s and t is the letter.

  • Examples

    Example 1:

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

    Example 2:

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

  • Note:
    You can assume that the string contains only lowercase letters.

  • Thinking

    标签:哈希映射
    1、首先判断两个字符串长度是否相等,不相等则直接返回 false
    2、若相等,则初始化 26 个字母哈希表,遍历字符串 s 和 t
    3、s 负责在对应位置增加,t 负责在对应位置减少
    4、如果哈希表的值都为 0,则二者是字母异位词
  • Code
class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;
        int[] alpha = new int[26];//用来存储结果
        for (int i = 0; i < s.length(); i++) {
           alpha[s.charAt(i) - 'a']++;
           alpha[t.charAt(i) - 'a']--;            
        }
        for (int i = 0; i < 26; i++) System.out.print(alpha[i]+" - ");
        for (int i = 0; i < 26; i++) if (alpha[i] != 0) return false;
        return true;
    }
    //测试用
    public static void  main(String[] args){
        String a = "aryuib";
        String c = "tryuib";
        String b = "rtuiby";
        Solution solution = new Solution();
        System.out.println(solution.isAnagram(c,b));
    }
}
  • Advanced:
    If the input string contains unicode characters how to do? Can you adjust your solution to deal with this situation?

  • Ideas:
    Under unicode, the characters too much, to be created in accordance with the length of the array length to unicode, which is obviously unreasonable to switch characters hash table dynamic storage appeared on ok

  • Code

    class Solution {
        public boolean isAnagram(String s, String t) {
            if (s.length() != t.length()|| s == null || t == null) {
                return false;
            }
            Map<Character, Integer> map = new HashMap<>();//哈希表存出现过的字符,以及出现次数
            for (int i = 0; i < s.length(); i++) {
                if (!map.containsKey(s.charAt(i))) {
                    map.put(s.charAt(i),1);
                } else {
                    int  integer = map.get(s.charAt(i))+1;
                    map.put(s.charAt(i),integer);
                }
            }
            for (int j = 0; j < t.length(); j++) {
                if (!map.containsKey(t.charAt(j))) {
                    return false;
                } else {
                    int  integer = map.get(t.charAt(j)) -1;
                    if (integer < 0)  return false;
                    map.put(t.charAt(j),integer);
                }
            }
            //遍历HashMap
            Iterator it = map.entrySet().iterator();
            for(Map.Entry<Character, Integer> entry: map.entrySet()){
               if(entry.getValue() != 0) return false;
            }
            return true;
        }
        //测试
        public static void  main(String[] args){
            String a = "aryuib";
            String c = "tryuib";
            String b = "rtuiby";
            Solution solution = new Solution();
            System.out.println(solution.isAnagram(a,b));
        }
    }
    
    

Guess you like

Origin blog.csdn.net/qq_40981804/article/details/91413773