字谜大全及答案100个
Problem statement: Given two strings, check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different.
问题陈述:给定两个字符串,请检查两个给定的字符串是否彼此相似 。 字符串的字谜是另一个包含相同字符的字符串,只有字符顺序可以不同。
For example, "act" and "cat" are anagram of each other.
例如, “ act”和“ cat”彼此相似。
Solution
解
Algorithm:
算法:
Anagram means both the string contains the same set of character, only their orders are different. Thus, in both the string the frequency of each letter must be the same if they are an anagram of each other. Thus our algorithm checks and compare frequency of each letter in both the strings.
Anagram表示两个字符串都包含相同的字符集,只是它们的顺序不同。 因此,在两个字符串中,每个字母的频率如果彼此相同,则必须相同。 因此,我们的算法会检查并比较两个字符串中每个字母的频率。
The strings to be anagram of each other, their length must be same.
字符串要彼此变位,其长度必须相同。
Let n1 be the length of first string & n2 be the length of second string. If (n1!=n2) Strings are not anagram. Return. Else Proceed to step2.
Initialize two arrays of length 26 to store frequency of characters in each string.
初始化两个长度为26的数组,以存储每个字符串中字符的频率。
array1[26]={0}, array2[26]={0}; //for the first string For i=1:n1 //n1 be the length of first string // for each letter of the string their corresponding array1[string1[i]-'a']++; //frequencies are being stored End for loop //for the second string For i=1:n2 //n2 be the length of second string // for each letter of the string their corresponding array2[string2[i]-'a']++; //frequencies are being stored End for loop
Comparison step
比较步骤
Compare the frequency of each letter in both the strings If all the letter in both of the string have same frequency (number of occurrence) Then they are anagrams of each other Else They are not anagrams of each other
Print result and return.
打印结果并返回。
C ++程序检查两个字符串是否是字谜 (C++ program to check whether two string is anagrams or not)
#include <bits/stdc++.h>
using namespace std;
int anagram(string s1,string s2){
int array1[26]={0},array2[26]={0};
//if string lengths are different
if(s1.length()!=s2.length())
return 0; //they are not anagrams
//for string1
for(int i=0;s1[i]!='\0';i++){
//storing frequency for each letter in the string
array1[s1[i]-'a']++;
}
//for string2
for(int i=0;s2[i]!='\0';i++){
//storing frequency for each letter in the string
array2[s2[i]-'a']++;
}
//comparison step
for(int i=0;i<26;i++){
// if any letter has different no of occurence,
// then strings are not anagrams
if(array1[i]!=array2[i])
return 0;
}
return 1;// else they are anagrams
}
int main()
{
int n;
string s1,s2;
//input the strings
cout<<"enter string1\n";
cin>>s1;
cout<<"enter string2\n";
cin>>s2;
if(anagram(s1,s2))
printf("strings are anagrams of each other\n");
else
printf("strings are not anagrams of each other\n");
return 0;
}
Output
输出量
First run:
enter string1
includehelp
enter string2
cnldeehpiul
strings are anagrams of each other
Second run:
enter string1
includehelp
enter string2
helpincludr
strings are not anagrams of each other
Explanation:
说明:
In the first output,
在第一个输出中
String1 ='includehelp'
String2='cnldeehpiul'
So, in the first string there is:
one 'I', one 'n', one 'c', two 'l', one 'u', one 'd', two 'e', one 'h' & one 'p'
in the second string there is:
one 'c', one 'n', two 'l', one 'd', two 'e', one 'h' & one 'p', one 'i' &one 'u'
Thus they are anagrams.
But in the second output,
但是在第二个输出中
The second string contains 'r' which is not in the first string, thus they are not anagrams.
第二个字符串包含“ r”,它不在第一个字符串中,因此它们不是字谜。
Recommended posts
推荐的帖子
翻译自: https://www.includehelp.com/icp/check-whether-two-string-is-anagrams-or-not.aspx
字谜大全及答案100个