java 字谜_计算字谜的出现次数
java 字謎
Problem statement:
問(wèn)題陳述:
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.
給定一個(gè)字符串S和一個(gè)單詞C ,返回該單詞在文本中的字謎出現(xiàn)的次數(shù) 。 字符串和單詞均為小寫(xiě)字母。
Examples:
例子:
Input:S=fororfrdofrC=forOutput:3Input:S=aabaabaaC=aabaOutput:4Example with explanation:
帶有說(shuō)明的示例:
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.
如果兩個(gè)單詞的長(zhǎng)度相同并且使用相同頻率的相同字符集,則兩個(gè)單詞彼此稱(chēng)為字謎。
"aba" and "baa" are anagrams since both have two a's and one b.
“ aba”和“ baa”是七巧板,因?yàn)樗鼈兌加袃蓚€(gè)a和一個(gè)b 。
"aba" and "bab" are not anagrams.
“ aba”和“ bab”不是字謎。
For the First example:
對(duì)于第一個(gè)示例:
Input:S=fororfrdofrC=forS:0 1 2 3 4 5 6 7 8 9 10f o r o r f r d o f rS(0,2): forThus S(0,2) is anagram of wS(3,5) : orfThus S(3,5) is anagram of wS(8,10) : ofrThus S(8,10) is anagram of wThus count of occurrences of anagrams: 3Pre-requisite:
先決條件:
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的或字謎檢測(cè)
String::substr(int i,int j): returns substring of length j from index i
String :: substr(int i,int j) :從索引i返回長(zhǎng)度為j的子字符串
Algorithm:
算法:
1. Set count=0;For i=0:i<a.length()-b.length()+1IF (check(a.substr(i,b.length()),b))count++;2. Print countSo 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.
所以這個(gè)想法很簡(jiǎn)單。 我們正在做的是滑動(dòng)窗口并檢查字謎。 Window只是每次提取的子字符串。 窗口長(zhǎng)度等于字長(zhǎng)。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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 mapFor (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 FUNCTIONSo 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(chǎn)的元素構(gòu)造一個(gè)容器。 然后,我們將映射銷(xiāo)毀以形成字符串b。 如果這樣的形成是可能的,則它們的弦是彼此的字謎。
C++ implementation:
C ++實(shí)現(xiàn):
#include <bits/stdc++.h> using namespace std;bool check(string a,string b){//checking anagramsmap<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 windowfor(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 字謎
總結(jié)
以上是生活随笔為你收集整理的java 字谜_计算字谜的出现次数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《蜀四贤咏》第十九句是什么
- 下一篇: 法的技能怎么有的看不到画面效果