当前位置:网站首页>【LeetCode-389】找不同
【LeetCode-389】找不同
2022-08-11 05:30:00 【Ring*】
6.8 找不同【389】
6.8.1 题目描述
给定两个字符串 s
和 t
,它们只包含小写字母。
字符串 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 ' ';
}
}
复杂度分析
- 时间复杂度: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 At−As 即代表了被添加的字符。
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);
}
}
复杂度分析
- 时间复杂度: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;
}
}
复杂度分析
- 时间复杂度: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';
}
}
边栏推荐
- OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
- C-8月1日-递归与动态内存管理
- Js method commonly used objects and attributes
- JVM tuning and finishing
- Some formulas for system performance and concurrency
- The whole process of Tinker access --- Compilation
- Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network
- USB URB
- JS advanced web page special effects (pink teacher notes)
- 手把手导入企业项目(快速完成本地项目配置)
猜你喜欢
【LeetCode-34】在排序数组中查找元素的第一个和最后一个位置
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
Visual studio2019 配置使用pthread
Day 84
【剑指offer系列】面试题日记(前期题)
一文看懂注解与反射
js 学习进阶(Dom部分 pink老师教学笔记)
Day 83
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
C语言-7月31日-指针的总结以及typedef关键字
随机推荐
C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子
The whole process of Tinker access --- configuration
字节(byte)和位(bit)
js learning advanced (event senior pink teacher teaching notes)
Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
JVM学习四:垃圾收集器与内存回收策略
JNI入门
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
OpenMLDB: Consistent production-level feature computing platform online and offline
【LeetCode-205】同构字符串
C语言-内存操作函数
Visual studio2019 配置使用pthread
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
编译异常解决
Matplotlib找不到字体,打印乱码
Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
C语言-6月10日-my_strcpy函数的编写
微信小程序云开发项目wx-store代码详解
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
JS事件循环机制