
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in C++ to check if a string can be obtained by rotating another string by two places
We are given two strings, a and b and the task is to find whether we can obtain the string b by rotating string a exactly two places in either an anticlockwise (left) or clockwise (right) direction.
Let's take an example scenario to understand the problem clearly ?
Scenario 1-
Input: a = "google", b = "legoog" Output: True Explanation: Rotating "google" two places anticlockwise gives "legoog", which matches string b. Thus, we return True.
Scenario 2-
Input 2: a = "tuorialst", b = "tutorials" Output: False Explanation: String "tuorialst" cannot be rotated two places in any direction to match "tutorials". Hence, the result is False.
Checking Two-Place String Rotation in C++
To solve the given problem, we check if one string matches another by rotating it exactly two places. There are two possible ways to do this rotation:
- Anticlockwise rotation: by moving the first two characters to the end of the string.
- Clockwise rotation: by moving the last two characters to the front of the string.
We apply both rotations to the first string and compare the results with the second string. If either one matches, we return true. Otherwise, we return false.
Following are the steps we take:
- First, we check if the lengths of the two strings a and b are equal. If they're not, we return false.
- Second, we handle short strings. If the length is less than or equal to 2, we directly compare them. If they are equal, we return true, otherwise false.
- Next, we perform a clockwise rotation on string a by moving its last two characters to the front.
- Then, we perform an anticlockwise rotation on string a by moving its first two characters to the end.
- After that, we compare both rotated versions of string a with string b.
- Finally, we return true if either of the rotations matches b. If neither matches, we return false.
C++ Program To Check String Rotation by Two Places
Below is a complete C++ program where we implement the steps discussed above to check whether one string can be obtained by rotating another string exactly two places.
#include <iostream> using namespace std; bool checkRotated(string a, string b) { // Step 1: Check length equality if (a.length() != b.length()) return false; // Step 2: Handle short strings (length ? 2) if (a.length() <= 2) return (a == b); int len = a.length(); // Step 3: Clockwise rotation (last 2 chars to front) string clockwise = a.substr(len - 2) + a.substr(0, len - 2); // Step 4: Anticlockwise rotation (first 2 chars to end) string anticlockwise = a.substr(2) + a.substr(0, 2); // Step 5 & 6: Compare with b and return result return (b == clockwise || b == anticlockwise); } int main() { string a = "google"; string b = "legoog"; if (checkRotated(a, b)) cout << "True\n"; else cout << "False\n"; return 0; }
Below is the output of the above program-
True
Time Complexity: O(n) because we create and compare rotated strings of length n.
Space Complexity: O(n) because we store these rotated strings.
Conclusion
In this article, we learned how to check if one string can be obtained by rotating another string exactly two places, either anticlockwise or clockwise. We applied both rotations and compared the results to see if they match.