389.找不同
题目:
给定两个字符串 s
和 t
,它们只包含小写字母。
字符串 t
由字符串 s
随机重排,然后在随机位置添加一个字母。
请找出在 t
中被添加的字母。
示例 1:
输入:s = "abcd", t = "abcde" 输出:"e" 解释:'e' 是那个被添加的字母。
示例 2:
输入:s = "", t = "y" 输出:"y"
提示:
0 <= s.length <= 1000
t.length == s.length + 1
s
和t
只包含小写字母
解析:
将字符串 s 中每个字符的 ASCII 码的值求和,得到 as;对字符串 t 同样的方法得到 At。
两者的差值 at−as即代表了被添加的字符。
假设 s = "abcd"
,t = "abcde"
,执行过程如下:
as = 'a' + 'b' + 'c' + 'd' = 97 + 98 + 99 + 100 = 394
at = 'a' + 'b' + 'c' + 'd' + 'e' = 97 + 98 + 99 + 100 + 101 = 495
差值 = at - as = 495 - 394 = 101
ASCII 值 101 对应的字符是 'e'
因此,函数会正确返回字符 e
。
代码:
#include <iostream>
#include<string>
using namespace std;
class Solution
{
public:
char findTheDifference(string s, string t)
{
int as = 0, at = 0; //初始化两个整数,分别存储字符串 s 和 t 的 ASCII 值之和
for(char ch: s) //遍历字符串 s 中的每一个字符
{
as += ch; //将字符的 ASCII 值加到 as 中
}
for(char ch: t) //遍历字符串 t 中的每一个字符
{
at += ch; //将字符的 ASCII 值加到 at 中
}
return at - as;
}//返回字符串t的 ASCII 值之和减去字符串 s 的 ASCII 值之和的差值,即为多出的那个字符
};
int main()
{
string s, t;
cin >> s >> t;
Solution sol;
cout<<sol.findTheDifference(s, t);
return 0;
}
28.找出字符串中第一个匹配项的下标
题目:
给你两个字符串 haystack
和 needle
,请你在 haystack
字符串中找出 needle
字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle
不是 haystack
的一部分,则返回 -1
。
示例 1:
输入:haystack = "sadbutsad", needle = "sad" 输出:0 解释:"sad" 在下标 0 和 6 处匹配。 第一个匹配项的下标是 0 ,所以返回 0 。
示例 2:
输入:haystack = "leetcode", needle = "leeto" 输出:-1 解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
提示:
1 <= haystack.length, needle.length <= 104
haystack
和needle
仅由小写英文字符组成
解析1:
我们可以让字符串 needle 与字符串 haystack 的所有长度为 m 的子串均匹配一次。
为了减少不必要的匹配,我们每次匹配失败即立刻停止当前子串的匹配,对下一个子串继续匹配。如果当前子串匹配成功,我们返回当前子串的开始位置即可。如果所有子串都匹配失败,则返回 −1。
代码1:暴力匹配
#include <iostream>
#include<string>
using namespace std;
class Solution
{
public:
int strStr(string haystack, string needle)
{
int n = haystack.size(), m = needle.size();
for (int i = 0; i + m <= n; i++)
{
bool flag = true;
for (int j = 0; j < m; j++)
{
if (haystack[i + j] != needle[j])
{
flag = false;
break;
}
}
if (flag)
{
return i;
}
}
return -1;
}
};
int main()
{
string h, n;
cin >> h >> n;
Solution sol;
cout<<sol.strStr(h, n);
return 0;
}
解析2:
第一部分是求 needle 部分的前缀函数,我们需要保留这部分的前缀函数值。
第二部分是求 haystack 部分的前缀函数,我们无需保留这部分的前缀函数值,只需要用一个变量记录上一个位置的前缀函数值即可。当某个位置的前缀函数值等于 m 时,说明我们就找到了一次字符串 needle 在字符串 haystack 中的出现(因为此时真前缀恰为字符串 needle,真后缀为以当前位置为结束位置的字符串 haystack 的子串),我们计算出起始位置,将其返回即可。
代码2:KMP算法
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Solution
{
public:
int strStr(string haystack, string needle)
{
int n = haystack.size(), m = needle.size();
if (m == 0)
{
return 0;
}
vector<int> pi(m);
for (int i = 1, j = 0; i < m; i++)
{
while (j > 0 && needle[i] != needle[j])
{
j = pi[j - 1];
}
if (needle[i] == needle[j])
{
j++;
}
pi[i] = j;
}
for (int i = 0, j = 0; i < n; i++)
{
while (j > 0 && haystack[i] != needle[j])
{
j = pi[j - 1];
}
if (haystack[i] == needle[j])
{
j++;
}
if (j == m)
{
return i - m + 1;
}
}
return -1;
}
};
int main()
{
string h, n;
cin >> h >> n;
Solution sol;
cout<<sol.strStr(h, n);
return 0;
}
242.有效的字母异位词
题目:
给定两个字符串 s
和 t
,编写一个函数来判断 t
是否是 s
的字母异位词。
注意:若 s
和 t
中每个字符出现的次数都相同,则称 s
和 t
互为字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram" 输出: true
示例 2:
输入: s = "rat", t = "car" 输出: false
提示:
1 <= s.length, t.length <= 5 * 104
s
和t
仅包含小写字母
解析:
t 是 s 的异位词等价于「两个字符串排序后相等」。因此我们可以对字符串 s 和 t 分别排序,看排序后的字符串是否相等即可判断。此外,如果 s 和 t 的长度不同,t 必然不是 s 的异位词。
代码:
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t)
{
if(s.length()!= t.length())
{
return false; //字符串长度不同,直接返回false
}
sort(s.begin(), s.end()); //对s字符串的字母进行排序
sort(t.begin(), t.end()); //对t字符串的字母进行排序
return s == t; //如果s == t,则返回true,否则返回false
}
};
int main()
{
string s, t;
cin >> s >> t;
Solution sol;
cout<<sol.isAnagram(s, t);
return 0;
}