Implementation in C: #include<stdio.h> #include<string.h> #include<stdlib.h> int isRotation(char *s1, char *s2) { int len = strlen(s1); if(len == strlen(s2) && len >0) { char *temp; void *ptr; temp = (char*)malloc(sizeof(char)*(len*2 +1)); temp[0] = '\0'; strcat(temp,s1); strcat(temp,s1); ptr = strstr(temp, s2); free(temp); if(ptr!= NULL) return 1; else return 0; } return 0; } void main() { char *s1 = "waterbottle"; char *s2 = "erbottlewat"; if( isRotation(s1, s2)) { printf("Yes, s2 is the rotation of s1."); } else { printf("No, s2 is not the rotation of s1."); } } Time Complexity: Complexity of this function depends on the implementation of strstr function.
Comments
Post a Comment