Leetcode刷题Day6(C#)

3. 无重复字符的最长子串

题目描述:给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

解题:

567. 字符串的排列

题目描述:

给你两个字符串 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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

没想到两个字母之间还可以相互加减。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值