字谜大全及答案100个_检查字谜(检查两个字符串是否是字谜)
字谜大全及答案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
推荐的帖子
Run-length encoding (find/print frequency of letters in a string)
游程编码(字符串中字母的查找/打印频率)
Count and Say sequence
计数并说出顺序
Longest Common Prefix
最长的公共前缀
Count Substrings
计数子串
Number following the pattern
跟随模式的数字
Next Permutation
下一个排列
Convert Ternary Expression to Binary Tree
将三元表达式转换为二叉树
Count of strings that can be formed using a, b and c under given constraints
在给定约束下可以使用a,b和c形成的字符串数
Minimum Number of Flips
最小翻转次数
Count Occurrences of Anagrams
计算字谜的出现次数
Rearrange a string
重新排列字符串
Print bracket number
列印支架编号
Longest Palindromic Subsequence
最长回文序列
Preorder to Postorder of BST
BST的预购到后购
Maximum difference of zeros and ones in binary string
二进制字符串中零和一的最大差
翻译自: https://www.includehelp.com/icp/check-whether-two-string-is-anagrams-or-not.aspx
字谜大全及答案100个
字谜大全及答案100个_检查字谜(检查两个字符串是否是字谜)相关推荐
- 100个小学生猜字谜大全及答案
100个小学生猜字谜大全及答案 1.字谜:山上还有山.猜一字,答案是:出 2.字谜:十张口,一颗心.猜一字,答案是:思 3.字谜:说它小,下边大,说它大,上边小.猜一字,答案是:尖 4.字谜:一只黑狗 ...
- 关于计算机的英语谜语,英语谜语大全及答案100个|英语谜语(50条)
[www.wzktys.com--小学生作文] 1. What is clean when it is black and dirty when it is white? 2. What has a ...
- cstring查找子字符串_动态规划6:两个字符串的最长连续公共子串
本文和前一篇:动态规划5-两个字符串的最长公共子序列类似,但公共子串必须是连续的,子序列不需要连续 字符串a,长度为m:a[1].a[2].a[3].a[4]....a[m] 字符串b,长度为n:b[ ...
- 找出两个字符串中所有共同的字符_面试中的两个字符串问题竟给我整懵了?!| 原力计划...
作者 | 一路向维 责编 | 王晓曼 出品 | CSDN博客 昨天收到通知,今天有个面试,一家魔都中型电商公司,名字咱就不说了.内心顿时踌躇满志.跃跃欲试,晚上还翻看面试题,做准备. 到了目的地之后, ...
- 好想学python怎么猜人名_猜人名的谜语大全及答案
猜人名的谜语大全及答案 猜人名的谜语精选 10 则: 1) 陇西布衣 ( 打一古代帝王 ) --谜底:李世民 2) 飞将军解甲而归 ( 打一现代作家 ) --谜底:李广田 3) 上下分离,驾驭不了. ...
- 1235813找规律第100个数_人教版一年级下册数学第1-8单元知识点梳理填空,附答案...
参考答案: 第一单元 认识图形 1.认识平面图形 2.平面图形的拼组 用相同的正方形.长方形或三角形可以分别拼成更大的正方形.长方形或三角形. 3.认识七巧板 七巧板是由1个正方形.1个平行四边形.5 ...
- 好想学python 怎么猜人物_猜人物谜语大全及答案
导读:小编根据大家的需要整理了一份关于<猜人物谜语大全及答案>的内容,具体内容: 谜语主要指暗射事物或文字等供人猜测的隐语,也可引申为蕴含奥秘的事物.下面由小编为大家推荐相关的,希望对你有 ...
- Jvm面试题及答案(300道 Jvm面试题大全带答案)
最新Jvm面试题及答案[附答案解析]Jvm面试题大全带答案,Jvm面试题最新面试题,Jvm面试题新答案已经全部更新完了,有些答案是自己总结的,也有些Jvm面试题答案是在网上搜集整理的.这些答案难免会存 ...
- Java面试题大全带答案 110道(持续更新)
本人发现网上虽然有不少Java面试题大全带答案,但第一未必全,第二未必有答案,第三虽然有答案,但未必能在面试中说,所以在本文里,会不断收集各种面试题,并站在面试官的立场上,给出我自己的答案 如果不背 ...
最新文章
- 【OpenCV 4开发详解】图像透视变换
- Google发布App Engine第二代运行时,提供Python 3.7和PHP 7.2支持
- css hover图片hover效果兼容ie8
- Android+Java中使用RSA加密实现接口调用时的校验功能
- 『数据库』怎样设计一个数据库
- 北航数值分析大作业_嫦娥5号在月球取土,有清华人的贡献吗?哪些学校贡献比较大?...
- 鸿蒙生态与苹果生态有什么区别,华为想用鸿蒙统一生态,苹果直接用芯片大一统,走到华为前面?...
- 梯度下降-单变量线性回归-理论+代码+解释
- 2019PMP考试专题资料大全
- 人工智能语音训练数据的制作方式?
- 顺序表基本操作归纳整理
- 屏幕缩放和Origin缩放不一致如何解决?
- Windows 7 专业版/家庭版 装(中文)语言包
- fpga时序相关—set_input_delay和set_output_delay
- 关于Rational Rose的使用(初学者)
- MySQL中like查询是否会使用索引
- 违禁词检索chrom扩展插件
- RocketMQTemplate 注入失败
- [Work Summary] 远程FTP下载文件到本地目录
- 离散度计算公式 python_SciPyTutorial-离散卷积convolve