• 【LeetCode-389】找不同


    6.8 找不同【389】

    6.8.1 题目描述

    给定两个字符串 st ,它们只包含小写字母。

    字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

    请找出在 t 中被添加的字母。
    在这里插入图片描述

    6.8.2 方法一:计数

    首先遍历字符串 s,对其中的每个字符都将计数值加 1;然后遍历字符串 t,对其中的每个字符都将计数值减 1。当发现某个字符计数值为负数时,说明该字符在字符串 t 中出现的次数大于在字符串 s 中出现的次数,因此该字符为被添加的字符。

    class Solution {
        public char findTheDifference(String s, String t) {
            int[] cnt = new int[26];
            for (int i = 0; i < s.length(); ++i) {
                char ch = s.charAt(i);
                cnt[ch - 'a']++;
            }
            for (int i = 0; i < t.length(); ++i) {
                char ch = t.charAt(i);
                cnt[ch - 'a']--;
                if (cnt[ch - 'a'] < 0) {
                    return ch;
                }
            }
            return ' ';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    复杂度分析

    • 时间复杂度:O(N),其中 N 为字符串的长度。
    • 空间复杂度: O ( ∣ Σ ∣ ) O(|\Sigma|) O(∣Σ∣),其中 Σ \Sigma Σ 是字符集,这道题中字符串只包含小写字母, ∣ Σ ∣ = 26 ∣ |\Sigma|=26∣ ∣Σ∣=26。需要使用数组对每个字符计数。

    6.8.3 方法二:求和

    将字符串 s 中每个字符的 ASCII 码的值求和,得到 A s A_s As;对字符串 t 同样的方法得到 A t A_t At。两者的差值 A t − A s A_t-A_s AtAs 即代表了被添加的字符。

    class Solution {
        public char findTheDifference(String s, String t) {
            int as = 0, at = 0;
            for (int i = 0; i < s.length(); ++i) {
                as += s.charAt(i);
            }
            for (int i = 0; i < t.length(); ++i) {
                at += t.charAt(i);
            }
            return (char) (at - as);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    复杂度分析

    • 时间复杂度:O(N)。
    • 空间复杂度:O(1)。

    6.8.4 方法三:位运算

    如果将两个字符串拼接成一个字符串,则问题转换成求字符串中出现奇数次的字符。类似于「136. 只出现一次的数字」,我们使用位运算的技巧解决本题。

    class Solution {
        public char findTheDifference(String s, String t) {
            int ret = 0;
            for (int i = 0; i < s.length(); ++i) {
                ret ^= s.charAt(i);
            }
            for (int i = 0; i < t.length(); ++i) {
                ret ^= t.charAt(i);
            }
            return (char) ret;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    复杂度分析

    • 时间复杂度:O(N)。
    • 空间复杂度:O(1)。

    6.8.5 my answer—排序/计数

    class Solution {
        // map计数
        public char findTheDifference(String s, String t) {
            Map<Character,Integer> map = new HashMap<>();
            char[] chars = s.toCharArray();
            char[] chart = t.toCharArray();
            for (char c : chars) {
                map.put(c,map.getOrDefault(c,0)+1);
            }
            for (char aChar : chart) {
                if(map.containsKey(aChar)){
                    map.put(aChar,map.getOrDefault(aChar,0)-1);
                }else {
                    return aChar;
                }
                if(map.get(aChar)<0)return aChar;
            }
            return '1';
        }
    
    	// 排序
        public char findTheDifference(String s, String t) {
            char[] chars = s.toCharArray();
            char[] chart = t.toCharArray();
            Arrays.sort(chars);
            Arrays.sort(chart);
            for (int i = 0; i < Math.max(chars.length, chart.length); i++) {
                if(i==chars.length)return chart[i];
                if(chars[i] != chart[i])return chart[i];
            }
            return '1';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    Pyside6:开发一个自适应的图片控件
    二叉树--经典面试题2
    进程和线程详解
    Python+selenium自动化生成测试报告
    面试经典 150 题 5 —(双指针)— 15. 三数之和
    彩虹外链网盘界面UI美化版超级简洁好看
    maltose-BSA 麦芽糖-牛血清白蛋白 BSA-PEG-maltose,牛血清白蛋白-PEG-麦芽糖
    基于内存的分布式NoSQL数据库Redis(六)AOF设计
    MFC_常见的消息,宏替换分析MFC消息映射
    Kaldi 入门使用教程
  • 原文地址:https://blog.csdn.net/xiaoguanglin/article/details/126213027