Find All Anagrams in a String

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求

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.

给定一个字符串s和一个非空字符串p,找到s中p的anagrams的所有起始索引。

字符串仅由小写英文字母组成,字符串s和p的长度不会大于20,100。

输出顺序无关紧要。

在这里插入图片描述

2,题目思路

对于这道题,首先给定两个字符串,其中一个字符串p类似于一个模板,判断第一个字符串s中有多少个字符和p中相同的单词,返回它们的索引值。

这道题的难点有两个:

  • 对两个单词的字符是否相等进行判断。
  • 滑动窗口。

对于第一个问题,一个比较常见到的思路,就是利用Hash表来进行。即将第二个单词p作为模板,把其中每个出现的字符统计一下。
而统计hash表也有两种方式:

  • 直接定义长度为26的hash,利用c - 'a’的方式映射到0-26上;
  • 定义一个长度为256的hash,让c的ASCII值作为索引。

这样,在遍历两个单词时,初始化Hash后,再找到一共需要判断的字符的个数——也就是p的长度。

遍历完一次,就可以判断两个单词的单词是否相同了。

对于第二个问题,滑动窗口。一般的解决办法是定义窗口的左右边界通过right++,到达窗口大小极限、left++的方式来实现窗口的滑动。

3,代码实现

int x = []() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    return 0;
}();

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> res = {};
        if(s.empty() || p.empty())
            return res;
        
        //ASCII作为索引
        vector<int> hash (256, 0);
        //初始化hash模板
        for(auto c : p)
            hash[c]++;
        
        //需要判断的总长度
        int sLen = s.size();
        //模板的长度,也就是需要判断的字符个数
        int count = p.size();
        
        int left = 0;
        int right = 0;
        
        while(right < sLen){
        	//如果值大于1,说明此时s[right]对应的字符在模板还有可对应的。
        	//于是,count也要减一,说明找到了对应的
        	//如果值为0,说明此时模板中已经没有可以与此事s[right]对应的字符了
            if(hash[s[right]] >= 1)
                count--;
            
            //该字符在模板中已经使用了,因此需要减一;
            //窗口向右移动一步        
            hash[s[right]]--;
            right++;
            
            //如果count变为了0,说明在规定的长度中找到了对应的单词
            //因此,将s中对应单词的开始索引、也就是left加入到res中      
            if(count == 0)
                res.push_back(left);
            
            //因为如果我们要向右滑动窗口,因此,此时最左边的字符就不会考虑
            //但是最左边的字符我们是已经经过了判断的,对hash表做了修改的
            //因此,我们需要对已经改变的hash表进行还原
            //之所以要进行hash[s[left]] >= 0的判断是因为,如果left对应的字符时出现在p中的,
            //那么,当时在计算count就会进行减1操作,
            //但是,我们此时已经滑动了窗口了,该字符已经不被考虑了
            //因此,我们需要对count进行一次还原
        	//经过观察我们可以看到,其实和right的操作是一种镜像操作
            if(right - left == p.size()){
                if(hash[s[left]] >= 0)
                    count++;
                hash[s[left]]++;
                left++;
            }  
        }
        return res;
        
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全排列是指将一个字符串中的字符进行重新排列,使得所有可能的排列都被考虑到。对于给定的字符串s,可以使用递归的方法来实现全排列。首先,选择一个字符作为排列的第一个字符,然后对剩余的字符进行全排列。这个过程可以通过不断交换字符的位置来实现。具体的代码实现可以参考以下示例代码: ```java public class StringPermutation { public void permutation(String s) { char\[\] array = s.toCharArray(); permutationHelper(array, 0, array.length - 1); } private void permutationHelper(char\[\] array, int start, int end) { if (start >= end) { System.out.println(new String(array)); } else { for (int i = start; i <= end; i++) { swap(array, i, start); permutationHelper(array, start + 1, end); swap(array, i, start); } } } private void swap(char\[\] array, int i, int j) { char temp = array\[i\]; array\[i\] = array\[j\]; array\[j\] = temp; } public static void main(String\[\] args) { StringPermutation permutation = new StringPermutation(); String s = "abc"; permutation.permutation(s); } } ``` 这段代码会输出字符串"abc"的所有全排列,即"abc"、"acb"、"bac"、"bca"、"cab"、"cba"。\[2\] 需要注意的是,全排列的时间复杂度为O(n!),其中n为字符串的长度。因此,对于较长的字符串,全排列可能会非常耗时。如果只需要判断一个字符串是否是另一个字符串的排列,可以使用类似于438. Find All Anagrams in a String的方法,只需要比较两个字符串中长度相同的子字符串所包含的字符是否一样即可。\[1\] #### 引用[.reference_title] - *1* [[LeetCode] 567. Permutation in String 字符串中的全排列](https://blog.csdn.net/weixin_30802171/article/details/97515238)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [字符串全排列问题](https://blog.csdn.net/weixin_41876155/article/details/81869743)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值