LeetCode389. Find the Difference

文章目录

一、题目

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Example 1:

Input: s = “abcd”, t = “abcde”
Output: “e”
Explanation: ‘e’ is the letter that was added.
Example 2:

Input: s = “”, t = “y”
Output: “y”

Constraints:

0 <= s.length <= 1000
t.length == s.length + 1
s and t consist of lowercase English letters.

二、题解

class Solution {
   
public:
    char findTheDifference(string s, string t) {
   
        unordered_map<char,int> map1;
        unordered_map<char,int> map2;
        for(auto x:s){
   
            map1[x]++;
        }
        for(auto x:t){
   
            map2[x]++;
        }
        for(int i = 0;i < 26;i++){
   
            if(map1['a' + i] != map2['a' + i]) return 'a' + i;
        }
        return 'a';
    }
};

相关推荐

  1. LeetCode389. Find the Difference

    2023-12-28 11:52:01       58 阅读
  2. Leetcode 389. Find the Difference

    2023-12-28 11:52:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 11:52:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 11:52:01       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 11:52:01       87 阅读
  4. Python语言-面向对象

    2023-12-28 11:52:01       96 阅读

热门阅读

  1. 模型部署之——ONNX模型转RKNN

    2023-12-28 11:52:01       52 阅读
  2. sql 随机排序优化

    2023-12-28 11:52:01       51 阅读
  3. flutter websocket发送ping包?

    2023-12-28 11:52:01       61 阅读
  4. 知识付费小程序如何搭建?

    2023-12-28 11:52:01       57 阅读
  5. Python语法知识的笔记

    2023-12-28 11:52:01       46 阅读
  6. django的gunicorn的异步任务执行

    2023-12-28 11:52:01       63 阅读
  7. 模板方法模式(Template Method)

    2023-12-28 11:52:01       54 阅读
  8. 如何设计前后端分离的系统架构?

    2023-12-28 11:52:01       50 阅读
  9. [云原生] Go并发基础

    2023-12-28 11:52:01       67 阅读
  10. 函数function的{}和return的含义

    2023-12-28 11:52:01       47 阅读
  11. 节假日计算器

    2023-12-28 11:52:01       71 阅读
  12. SQL高级:窗口函数

    2023-12-28 11:52:01       63 阅读