C++ Program to Check if a string can be obtained by rotating another string 2 places Last Updated : 23 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 1- There can be only two cases: a) Clockwise rotated b) Anti-clockwise rotated 2- If clockwise rotated that means elements are shifted in right. So, check if a substring[2.... len-1] of string2 when concatenated with substring[0,1] of string2 is equal to string1. Then, return true. 3- Else, check if it is rotated anti-clockwise that means elements are shifted to left. So, check if concatenation of substring[len-2, len-1] with substring[0....len-3] makes it equals to string1. Then return true. 4- Else, return false. Below is the implementation of the above approach. C++ // C++ program to check if a string // is two time rotation of another string. #include<bits/stdc++.h> using namespace std; // Function to check if string2 is // obtained by string 1 bool isRotated(string str1, string str2) { if (str1.length() != str2.length()) return false; if(str1.length()<2){ return str1.compare(str2) == 0; } string clock_rot = ""; string anticlock_rot = ""; int len = str2.length(); // Initialize string as anti-clockwise // rotation anticlock_rot = anticlock_rot + str2.substr(len-2, 2) + str2.substr(0, len-2) ; // Initialize string as clock wise // rotation clock_rot = clock_rot + str2.substr(2) + str2.substr(0, 2) ; // check if any of them is equal // to string1 return (str1.compare(clock_rot) == 0 || str1.compare(anticlock_rot) == 0); } // Driver code int main() { string str1 = "geeks"; string str2 = "eksge"; isRotated(str1, str2) ? cout << "Yes" : cout << "No"; return 0; } Output: Yes Time Complexity: O(n), where n is the size of the given string. Please refer complete article on Check if a string can be obtained by rotating another string 2 places for more details! Comment More infoAdvertise with us K kartik Follow Improve Article Tags : C++ Amazon Accolite rotation Practice Tags : AccoliteAmazonCPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Primâs Algorithm for Minimum Spanning Tree (MST) Primâs algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.The algorithm starts with an empty spanning tree. The idea is to maintain two sets o 15+ min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Maximum Subarray Sum - Kadane's Algorithm Given an integer array arr[], find the subarray (containing at least one element) which has the maximum possible sum, and return that sum.Note: A subarray is a continuous part of an array.Examples:Input: arr[] = [2, 3, -8, 7, -1, 2, 3]Output: 11Explanation: The subarray [7, -1, 2, 3] has the largest 8 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Heap Sort - Data Structures and Algorithms Tutorials Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use 14 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read Like