一个检查字符串是否相互旋转的程序?
在这里,我们将看到一个程序,该程序可以判断两个字符串是否彼此旋转。字符串的旋转就像-
假设两个字符串是S1='HELLO'和S2='LOHEL',那么它们是彼此旋转的。将HELLO旋转到左侧三个位置,即为LOHEL。
为了解决这个问题,我们将第一个字符串与其自身连接起来,然后检查第二个字符串是否存在于连接的字符串中。因此,对于HELLO,将是HEL
算法
isRotation(str1,str2)
begin
if lengths of str1, and str2 are not same then return false;
temp := concatenate str1 with str1 itself
if temp contains str2, then
return true
otherwise return false
end示例
#include<iostream>
using namespace std;
bool isRotation(string str1, string str2){
if(str1.length() != str2.length())
return false;
string con_str = str1 + str1;
if(con_str.find(str2) != string::npos){
return true;
} else {
return false;
}
}
main() {
string str1, str2;
cout << "Enter two strings: ";
cin >> str1 >> str2;
if(isRotation(str1, str2)){
cout << "Two strings are rotation of each other";
} else {
cout << "Two strings are not rotation of each other";
}
}输出结果
Enter two strings: STACK CKSTA Two strings are rotation of each other
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练