Find All Anagrams in a String问题及解法

本文介绍了一种使用滑动窗口的方法来寻找一个字符串中所有指定字符串的字母异位词的起始索引。通过逐步移动窗口并更新计数器,可以有效地找到所有匹配项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

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.

示例:

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".
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".
问题分析:

求解s中所有以p颠倒顺序的所成子串索引值,我们可以用滑动窗口法来求解。

1.以p的长度为窗口长度,统计窗口内出现s和p的字母的次数sv和pv

2.在s中每次窗口向右滑动一个字母,统计该字母的次数,在sv中除去左边的一个字母,以维持滑动窗口大小不变

3.比较sv和pv是否相同,若相同,则把s中对应的索引添加进结果集中去。


过程详见代码:

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> pv(26,0), sv(26,0), res;
        if(s.size() < p.size())
           return res;
        // fill pv, vector of counters for pattern string and sv, vector of counters for the sliding window
        for(int i = 0; i < p.size(); ++i)
        {
            ++pv[p[i]-'a'];
            ++sv[s[i]-'a'];
        }
        if(pv == sv)
           res.push_back(0);

        //here window is moving from left to right across the string. 
        //window size is p.size(), so s.size()-p.size() moves are made 
        for(int i = p.size(); i < s.size(); ++i) 
        {
             // window extends one step to the right. counter for s[i] is incremented 
            ++sv[s[i]-'a'];
            
            // since we added one element to the right, 
            // one element to the left should be forgotten. 
            //counter for s[i-p.size()] is decremented
            --sv[s[i-p.size()]-'a']; 

            // if after move to the right the anagram can be composed, 
            // add new position of window's left point to the result 
            if(pv == sv)  
               res.push_back(i-p.size()+1);
        }
        return res;
    }
};


在C++中,要找到列表中独特的同字母异序词(anagrams),你需要首先定义一个函数来检查两个字符串是否是彼此的变位词(anagram),然后遍历整个单词列表,使用哈希集合或者`std::unordered_set`来存储已经遇到过的anagram,并判断每个新遇到的单词是否与已存在的单词是anagram。下面是一个简单的示例程序: ```cpp #include <iostream> #include <vector> #include <string> #include <unordered_set> // Function to check if two strings are anagrams bool isAnagram(const std::string& s1, const std::string& s2) { std::sort(s1.begin(), s1.end()); std::sort(s2.begin(), s2.end()); return s1 == s2; } // Function to find unique anagrams in a list std::vector<std::string> findUniqueAnagrams(std::vector<std::string>& wordList) { std::unordered_set<std::string> uniqueAnagrams; for (const auto& word : wordList) { // Skip empty strings and duplicates if (word.empty() || uniqueAnagrams.find(word) != uniqueAnagrams.end()) continue; // Check each word with others for (auto it = wordList.begin(); it != wordList.end(); ++it) { if (isAnagram(word, *it)) { uniqueAnagrams.insert(*it); } } } return std::vector<std::string>(uniqueAnagrams.begin(), uniqueAnagrams.end()); } int main() { std::vector<std::string> words = {"listen", "silent", "triangle", "integral", "act", "cat"}; std::vector<std::string> uniqueAnagramSet = findUniqueAnagrams(words); std::cout << "Unique anagrams found:\n"; for (const auto& anagram : uniqueAnagramSet) { std::cout << anagram << "\n"; } return 0; } ``` 在这个程序中,我们首先创建了一个哈希集合`uniqueAnagrams`来存储唯一的anagram。接着,在`findUniqueAnagrams`函数中,对每个单词,如果它是一个新的非空单词并且是其他某个单词的anagram,我们就将其添加到集合中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值