Member-only story
Interview question: Use Python to determine if two strings are a rotation mutually
String manipulation problems are very common problems that an applicant will be asked to solve in a coding interview. In an interview the applicant will be provided with two strings and will be asked to develop an algorithm to determine if they are rotations of each other.
A string is said to be a rotation of another string if:-
- Both strings have equal length and are composed of the same characters.
- The second string can be obtained by rotating the first string around a certain character.
The psuedocode to find out if two strings are rotations of each other is:-
- Define the two strings, s1 and 22, which are going to be tested for rotation mutuality.
- Defined the function, checkRotation, which will accept the two strings as input.
- Defined the variable, temp, which is an empty string.
- If the two strings are not the same length then the two strings are not mutually exclusive.
- Add the string, s1, to itself.
- If the string, s2, is in temp then the two strings are a rotation mutually, otherwise they are not.
The code in Python to see if two strings are rotation mutually is below:-