java 字谜_计算字谜的出现次数

java 字谜

Problem statement:

问题陈述:

Given a string S and a word C, return the count of the occurrences of anagrams of the word in the text. Both string and word are in lowercase letter.

给定一个字符串S和一个单词C ,返回该单词在文本中的字谜出现的次数 。 字符串和单词均为小写字母。

Examples:

例子:

    Input:
    S=fororfrdofr
    C=for

    Output:
    3
    
    Input:
    S=aabaabaa
    C=aaba
    
    Output:
    4

Example with explanation:

带有说明的示例:

Anagrams:

字谜:

Two words are known to be anagrams of each other if they are of same length & use same set of characters with same frequencies.

如果两个单词的长度相同并且使用相同频率的相同字符集,则两个单词彼此称为字谜。

"aba" and "baa" are anagrams since both have two a's and one b.

“ aba”“ baa”是七巧板,因为它们都有两个a和一个b

"aba" and "bab" are not anagrams.

“ aba”“ bab”不是字谜。

For the First example:

对于第一个示例:

    Input:
    S=fororfrdofr
    C=for

    S:
    0	1	2	3	4	5	6	7	8	9	10
    f	o	r	o	r	f	r	d	o	f	r

    S(0,2): for
    Thus S(0,2) is anagram of w

    S(3,5) : orf
    Thus S(3,5) is anagram of w

    S(8,10) : ofr
    Thus S(8,10) is anagram of w

    Thus count of occurrences of anagrams: 3

Pre-requisite:

先决条件:

  1. FUNCTION to check whether to word is anagram or not

    检查单词是否为字谜的功能

    Check(string s1, string s2): returns true or false based or anagram detection

    Check(字符串s1,字符串s2) :返回基于true或false的或字谜检测

  2. String::substr(int i,int j): returns substring of length j from index i

    String :: substr(int i,int j) :从索引i返回长度为j的子字符串

Algorithm:

算法:

    1.  Set count=0;
        For i=0:i<a.length()-b.length()+1
            IF (check(a.substr(i,b.length()),b))
            count++;
    2.  Print count

So the idea is pretty simple. What we are doing is to slide the window and check for anagrams. Window is nothing but the substring being extracted every time. Window length is equal to the word length.

所以这个想法很简单。 我们正在做的是滑动窗口并检查字谜。 Window只是每次提取的子字符串。 窗口长度等于字长。

To check anagrams

检查字谜

FUNCTION check(string a, string b)

1.  Declare a map 
   
   
    
     m; //to store frequency of each character
2.  For string abuild the map
    For (int i=0;i<a.length();i++)
        m[a[i]]++;
    END FOR
3.  For string bdestruct the map.
    For (int i=0;i<b.length();i++)
        m[b[i]]--;
    END FOR
4.  If map is totally destructed that means all key has value perfectly 0 
        return true;
    Else 
        Return false.

END FUNCTION

   
   

So this basically means, we are constructing a container using the elements of string a. Then we are destructing the map to form string b. If such formation is possible they strings are anagrams of each other.

因此,这基本上意味着,我们正在使用字符串a的元素构造一个容器。 然后,我们将映射销毁以形成字符串b。 如果这样的形成是可能的,则它们的弦是彼此的字谜。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

bool check(string a,string b){
	//checking anagrams
	map<char,int> m;
	for(int i=0;i<a.length();i++)
		m[a[i]]++;
	for(int i=0;i<b.length();i++)
		m[b[i]]--;
	for(auto it=m.begin();it!=m.end();it++)
		if(it->second!=0)
			return false;
	return true;
}

int countNoOfAnagram(string a,string b){
	int count=0;
	//sliding window
	for(int i=0;i<a.length()-b.length()+1;i++){
		if(check(a.substr(i,b.length()),b))
			count++;
	}
	return count;
}

int main()
{
	string S,C;
	
	cout<<"Enter string S and word C\n";
	cin>>S>>C;
	
	cout<<"No of anagrams: "countNoOfAnagram(S,C)<<endl;
	
	return 0;
}

Output

输出量

Enter string S and word C
fororfrdofr
for
No of anagrams: 3


翻译自: https://www.includehelp.com/icp/count-occurrences-of-anagrams.aspx

java 字谜

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值