题目描述:给定一个字符串 s
,请你找出其中不含有重复字符的 最长子串 的长度。
解题:
题目描述:
给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。换句话说,s1 的排列之一是 s2 的 子串 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutation-in-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题目解答:
想了半天,把自己绕进去了,还是没做出来,就抄了一个代码先:
public class Solution {
public bool CheckInclusion(string s1, string s2) {
char[] pattern = s1.ToCharArray();
char[] text = s2.ToCharArray();
int pLen = s1.Length;
int tLen = s2.Length;
int[] pFreq = new int[26];
int[] winFreq = new int[26];
for (int i = 0; i < pLen; i++)
{
pFreq[pattern[i] - 'a']++;
}
int pCount = 0;//有几个不同的字符
for (int i = 0; i < 26; i++)
{
if (pFreq[i] > 0)
{
pCount++;
}
}
int left = 0;
int right = 0;
// 当滑动窗口中的某个字符个数与 s1 中对应相等的时候才计数
int winCount = 0;
while (right < tLen)
{
if (pFreq[text[right] - 'a'] > 0)
{
winFreq[text[right] - 'a']++;
if (winFreq[text[right] - 'a'] == pFreq[text[right] - 'a'])
{
winCount++;
}
}
right++;
while (pCount == winCount)
{
if (right - left == pLen)
{
return true;
}
if (pFreq[text[left] - 'a'] > 0)
{
winFreq[text[left] - 'a']--;
if (winFreq[text[left] - 'a'] < pFreq[text[left] - 'a'])
{
winCount--;
}
}
left++;
}
}
return false;
}
}
作者:pyoungest
链接:https://leetcode-cn.com/problems/permutation-in-string/solution/zi-ji-ge-mei-xie-chu-lai-can-kao-de-ti-j-x7oa/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
没想到两个字母之间还可以相互加减。。。