📌  相关文章
📜  计算字谜的出现次数

📅  最后修改于: 2021-04-29 09:42:41             🧑  作者: Mango

给定一个单词和一个文本,返回该单词在文本中出现的字谜的计数(例如:for,forr,rof等的for单词的字谜)

例子:

Input : forxxorfxdofr
        for
Output : 3
Explanation : Anagrams of the word for - for, orf, 
ofr appear in the text and hence the count is 3.

Input : aabaabaa
        aaba
Output : 4
Explanation : Anagrams of the word aaba - aaba, 
abaa each appear twice in the text and hence the
count is 4.

一种简单的方法是从字符串的开头开始遍历,考虑子字符串的长度等于给定单词的长度,然后检查该子字符串是否具有单词的所有字符。

C++
// A Simple C++ program to count anagrams of a
// pattern in a text.
#include
using namespace std;
 
// Function to find if two strings are equal
bool areAnagram(string s1, string s2)
{
    map m;
    for(int i = 0; i < s1.length(); i++)
        m[s1[i]]++;
         
    for(int i = 0; i < s2.length(); i++)
        m[s2[i]]--;
         
    for(auto it = m.begin(); it != m.end(); it++)
        if(it -> second != 0)
            return false;
             
        return true;
}
 
int countAnagrams(string text, string word)
{
     
    // Initialize result
    int res = 0;
    for(int i = 0;
            i < text.length() - word.length() + 1;
            i++)
    {
         
        // Check if the word and substring are
        // anagram of each other.
        if (areAnagram(text.substr(i, word.length()),
                                      word))
            res++;
    }
    return res;
}
 
// Driver Code
int main()
{
    string text = "forxxorfxdofr";
    string word = "for";
     
    cout << countAnagrams(text, word);
     
    return 0;
}
 
// This code is contributed by probinsah


Java
// A Simple Java program to count anagrams of a
// pattern in a text.
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to find if two strings are equal
    static boolean araAnagram(String s1,
                              String s2)
    {
        // converting strings to char arrays
        char[] ch1 = s1.toCharArray();
        char[] ch2 = s2.toCharArray();
 
        // sorting both char arrays
        Arrays.sort(ch1);
        Arrays.sort(ch2);
 
        // Check for equality of strings
        if (Arrays.equals(ch1, ch2))
            return true;
        else
            return false;
    }
 
    static int countAnagrams(String text, String word)
    {
        int N = text.length();
        int n = word.length();
 
        // Initialize result
        int res = 0;
 
        for (int i = 0; i <= N - n; i++) {
 
            String s = text.substring(i, i + n);
 
            // Check if the word and substring are
            // anagram of each other.
            if (araAnagram(word, s))
                res++;
        }
     
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String text = "forxxorfxdofr";
        String word = "for";
        System.out.print(countAnagrams(text, word));
    }
}


Java
// An efficient Java program to count anagrams of a
// pattern in a text.
import java.io.*;
import java.util.*;
 
public class GFG {
    final static int MAX_CHAR = 256
 
    // Function to find if two strings are equal
    static boolean isCountZero(int[] count)
    {
        for (int i = 0; i < MAX_CHAR; i++)
            if (count[i] != 0)
                return false;
        return true;
    }
 
    static int countAnagrams(String text, String word)
    {
        int N = text.length();
        int n = word.length();
 
        // Check for first window. The idea is to
        // use single count array to match counts
        int[] count = new int[MAX_CHAR];
        for (int i = 0; i < n; i++)
            count[word.charAt(i)]++;
        for (int i = 0; i < n; i++)
            count[text.charAt(i)]--;
 
        // If first window itself is anagram
        int res = 0;
        if (isCountZero(count))
            res++;
 
        for (int i = n; i < N; i++) {
 
            // Add last character of current
            // window
            count[text.charAt(i)]++;
 
            // Remove first character of previous
            // window.
            count[text.charAt(i - n)]--;
 
            // If count array is 0, we found an
            // anagram.
            if (isCountZero(count))
                res++;
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String text = "forxxorfxdofr";
        String word = "for";
        System.out.print(countAnagrams(text, word));
    }
}


输出:
3

一个有效的解决方案是使用计数数组检查字谜,我们可以使用滑动窗口概念从O(1)时间的前一个窗口构造当前计数窗口。

Java

// An efficient Java program to count anagrams of a
// pattern in a text.
import java.io.*;
import java.util.*;
 
public class GFG {
    final static int MAX_CHAR = 256
 
    // Function to find if two strings are equal
    static boolean isCountZero(int[] count)
    {
        for (int i = 0; i < MAX_CHAR; i++)
            if (count[i] != 0)
                return false;
        return true;
    }
 
    static int countAnagrams(String text, String word)
    {
        int N = text.length();
        int n = word.length();
 
        // Check for first window. The idea is to
        // use single count array to match counts
        int[] count = new int[MAX_CHAR];
        for (int i = 0; i < n; i++)
            count[word.charAt(i)]++;
        for (int i = 0; i < n; i++)
            count[text.charAt(i)]--;
 
        // If first window itself is anagram
        int res = 0;
        if (isCountZero(count))
            res++;
 
        for (int i = n; i < N; i++) {
 
            // Add last character of current
            // window
            count[text.charAt(i)]++;
 
            // Remove first character of previous
            // window.
            count[text.charAt(i - n)]--;
 
            // If count array is 0, we found an
            // anagram.
            if (isCountZero(count))
                res++;
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String text = "forxxorfxdofr";
        String word = "for";
        System.out.print(countAnagrams(text, word));
    }
}
输出:
3