【CODE】字符串相关

567. Permutation in String

Medium

105652Add to ListShare

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.

Example 1:

Input: s1 = "ab" s2 = "eidbaooo"
Output: True
Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

Input:s1= "ab" s2 = "eidboaoo"
Output: False

Note:

  1. The input strings only contain lower case letters.
  2. The length of both given strings is in range [1, 10,000].
class Solution {
public:
    /*Runtime: 12 ms, faster than 66.73% of C++ online submissions for Permutation in String.
    Memory Usage: 7.5 MB, less than 100.00% of C++ online submissions for Permutation in String.
    使用滑动窗口Sliding Window的思想来做,可以使用两个哈希表来做,
或者是使用一个哈希表配上双指针来做。我们先来看使用两个哈希表来做的情况,
我们先来分别统计s1和s2中前n1个字符串中各个字符出现的次数,
其中n1为字符串s1的长度,这样如果二者字符出现次数的情况完全相同,
说明s1和s2中前n1的字符互为全排列关系,那么符合题意了,直接返回true。
如果不是的话,那么我们遍历s2之后的字符,对于遍历到的字符,对应的次数加1,
由于窗口的大小限定为了n1,所以每在窗口右侧加一个新字符的同时就要在窗口左侧去掉一个字符,
每次都比较一下两个哈希表的情况,如果相等,说明存在.*/
    bool checkInclusion(string s1, string s2) {
        vector<int> mp1(30),mp2(30);
        if(s1.size()>s2.size()) return false;
        for(int i=0;i<s1.size();i++){
            mp1[s1[i]-'a']++;
            mp2[s2[i]-'a']++;
        }
        if(mp1==mp2) return true;
        for(int i=s1.size();i<s2.size();i++){
            mp2[s2[i-s1.size()]-'a']--;
            mp2[s2[i]-'a']++;
            if(mp1==mp2) return true;
        }
        return false;
    }
};

438. Find All Anagrams in a String

Medium

2300161Add to ListShare

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:

Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
class Solution {
public:
    /*Runtime: 36 ms, faster than 56.42% of C++ online submissions for Find All Anagrams in a String.
    Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Find All Anagrams in a String.*/
    vector<int> findAnagrams(string s, string p) {
        if(s.size()<p.size()) return {};
        vector<int> mp1(128),mp2(128);//mp1:s,mp2:p
        vector<int> res;
        for(int i=0;i<p.size();i++){
            mp1[s[i]]++;mp2[p[i]]++;
        }
        if(mp1==mp2) res.push_back(0);
        for(int i=p.size();i<s.size();i++){
            mp1[s[i]]++;//处理的是谁的数据,改变的就是谁的mp
            mp1[s[i-p.size()]]--;
            if(mp2==mp1) res.push_back(i-p.size()+1);
        }
        return res;
    }
};
class Solution {
public:
    /*Runtime: 28 ms, faster than 93.26% of C++ online submissions for Find All Anagrams in a String.
    Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Find All Anagrams in a String.
    cnt表示剩余的需要匹配的p中元素个数
    维护左右指针,如果右边指针所指的值在p中也有,就将cnt--
    如果cnt减为0,将左边指针加入结果
    判断左右指针距离,如果左右指针距离为p的长度,说明窗口中多一个
    如果左边指针大于等于0,说明左边指针的指向在p中有,缩小窗口,cnt++*/
    vector<int> findAnagrams(string s, string p) {
        if(s.size()<p.size()) return {};
        vector<int> mp(128);
        vector<int> res;
        int cnt=p.size(),left=0,right=0;
        for(int i=0;i<p.size();i++){
            mp[p[i]]++;
        }
        while(right<s.size()){
            if(mp[s[right++]]-- >= 1) cnt--;
            if(cnt==0) res.push_back(left);
            if(right-left==p.size() && mp[s[left++]]++ >=0) cnt++;
        }
        return res;
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值