
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Write a program in C++ to check if a string can be obtained by rotating another string by two places
Suppose we’ve given two strings ‘a’ and ‘b’, the task is to find whether we can obtain the string ‘b’ by rotating string ‘a’ exactly by two places in an anticlockwise or clockwise direction. For example,
Input-1 −
a = google b = legoog
Output −
True
Explanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.
Input-2 −
a = tuorialst b = tutorials
Output −
False
Explanation- String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.
The approach used to solve this problem
For the given two strings, we have two cases in this approach −
For Anticlockwise rotation
For Clockwise rotation
First, if the length of both strings is different, then return false. Otherwise, if the length of both strings is less than or equal to ‘2’, then we will return True.
In other cases, we will check if the substring of string ‘b’ by rotating two places in an anticlockwise direction is equal to string ‘a’, then we will return True. Otherwise, False.
Similarly, if by rotating string ‘b’ in a clockwise direction by two places, it becomes equal to the string ‘a’, then return True, otherwise, we will return false.
Take two Input strings ‘a’ and ‘b’
A Boolean function checkRotated( string a, string b) takes two strings ‘a’ and ‘b’, and returns if they are equal by rotating string ‘b’ in an anticlockwise or clockwise direction.
Check the length of the string ‘a’ and string ‘b’.
Find the substring of string ‘b’ by rotating two places anticlockwise.
Now check if the resultant substring is equal to string ‘a’ or not and return true if it equals.
Find the substring of string ‘b’ by rotating it in two places in a clockwise direction.
Now check if the resultant substring is equal to string ‘a’ or not and return true if it equals.
If the strings are not equal, then return false.
Example
#include<bits/stdc++.h> using namespace std; bool checkRotated(string str1, string str2){ if (str1.length() != str2.length()) return false; if(str1.length() <= 2 || str2.length() <= 2) return (str1 == str2); string s1= str2.substr(str2.size()-2, str2.size()); string s2= str2.substr(0,str2.size()-2); string s3= s1+s2; if(s3==str1) return true; string s4= str2.substr(2,str2.size()); string s5= str2.substr(0,2); string s6= s4+s5; if(s6==str1) return true; return false; } int main(){ string a= "google"; string b="legoog"; cout<<checkRotated(a,b)<<endl; return 0; }
Output
If we will run the above code, it will print the output as,
1
Since the output of the above code is “True”, it will print ‘1’.
- Related Articles
- Write a program in Java to check if a string can be obtained by rotating another string by 2 places
- Check if a string can be obtained by rotating another string 2 places in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Check if a string can be repeated to make another string in Python
- Program to check one string can be converted to another by removing one element in Python
- Check If a String Can Break Another String in C++
- Check if a given string can be formed by two other strings or their permutations
- Check if a string can be formed from another string using given constraints in Python
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a string can become empty by recursively deleting a given sub-string in Python
- Check if a string can become empty by recursively deleting a given sub-string in C++
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- String slicing in Python to check if a can become empty by recursive deletion
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Check if a two-character string can be made using given words in Python
