Charlie has a magic mirror. The mirror shows right rotated versions of a given word.
For example, in the word "sample", if we start with "p", we get the right rotated word "plesam".
Create a function isSameReflection, which takes in two strings.
It returns 1 if the strings are right rotations of the same word and -1 otherwise
Both string arguments will strictly contain characters 'a' - 'z'
Assistant
To solve this problem, we need to determine whether two given strings are right rotations of the same original word.
Understanding Right Rotation
A right rotation of a string involves moving characters from the end of the string to the beginning. For example, consider the string "sample":
Original: "sample"
Right rotated by 1: "esampl"
Right rotated by 2: "lesamp"
Right rotated by 3: "plesam"
... and so on.
Given two strings, the goal is to check if both can be derived by right-rotating the same original string. This is equivalent to checking whether one string is a rotation of the other.
Approach
Check Lengths: If the two strings have different lengths, they cannot be rotations of the same word.
Concatenate: Concatenate the first string with itself. This new string will contain all possible rotations of the original string.
Search: Check if the second string is a substring of the concatenated string. If it is, then the two strings are rotations of the same original word.
Return Result: Return 1 if they are rotations, otherwise return -1.
Implementation in C
Below is the C implementation of the isSameReflection function based on the above approach, along with a main function to demonstrate its usage.
c
#include <stdio.h>
#include <string.h>
// Function to check if two strings are right rotations of the same word
int isSameReflection(const char *s1, const char *s2) {
// Check for NULL pointers
if (s1 == NULL || s2 == NULL) {
return -1;
}
int len1 = strlen(s1);
int len2 = strlen(s2);
// If lengths differ, they cannot be rotations
if (len1 != len2) {
return -1;
}
// If both strings are empty, they are rotations of each other
if (len1 == 0) {
return 1;
}
// Create a new string that is s1 concatenated with itself
char concatenated[2 * len1 + 1]; // +1 for null terminator
strcpy(concatenated, s1);
strcat(concatenated, s1);
// Check if s2 is a substring of the concatenated string
if (strstr(concatenated, s2) != NULL) {
return 1;
} else {
return -1;
}
}
// Helper function to test isSameReflection
void testIsSameReflection(const char *s1, const char *s2, int expected) {
int result = isSameReflection(s1, s2);
printf("isSameReflection(\"%s\", \"%s\") = %d | Expected: %d | %s\n",
s1, s2, result, expected, (result == expected) ? "PASS" : "FAIL");
}
int main() {
// Test cases
testIsSameReflection("sample", "plesam", 1); // Right rotation
testIsSameReflection("abcde", "cdeab", 1); // Right rotation
testIsSameReflection("abcde", "abced", -1); // Not a rotation
testIsSameReflection("aaaa", "aaaa", 1); // Same strings
testIsSameReflection("", "", 1); // Both empty
testIsSameReflection("a", "a", 1); // Single character
testIsSameReflection("abcd", "dabc", 1); // Right rotation by 1
testIsSameReflection("abcd", "cdab", 1); // Right rotation by 2
testIsSameReflection("abcd", "bcda", 1); // Right rotation by 3
testIsSameReflection("abcd", "acbd", -1); // Invalid rotation
testIsSameReflection("rotation", "tationro", 1); // Right rotation
testIsSameReflection("rotation", "otationr", 1); // Right rotation
testIsSameReflection("rotation", "rrotaiont", -1);// Not a rotation
return 0;
}
Explanation of the Code
Function isSameReflection:
Input Validation: Checks if either of the input strings is NULL. If so, returns -1.
Length Check: If the lengths of the two strings differ, they cannot be rotations of the same word.
Empty Strings: If both strings are empty, they are trivially rotations of each other.
Concatenation: Creates a new string concatenated which is the first string concatenated with itself. This contains all possible rotations of s1.
Substring Search: Uses strstr to check if s2 is a substring of concatenated. If it is, returns 1; otherwise, returns -1.
Function testIsSameReflection:
A helper function to test various cases by comparing the function's output with the expected result.
main Function:
Contains multiple test cases to validate the correctness of the isSameReflection function.
Sample Output
When you run the above program, you should get the following output:
This solution efficiently checks whether two strings are right rotations of the same original word by leveraging string concatenation and substring search. The time complexity is linear with respect to the length of the strings, making it suitable for large inputs.