Array ( [0] => [1] => questions [2] => Strings [3] => Equal-Strings )
Given two strings K and L. You have to check if strings are rotations of each other or not.
First line contains the string K. Next line contains the string L.
Print Yes if they are same else print No.
educate cateedu
Yes
never everr
No
learn arnln
Yes
Login to see Discussion
Approach 1: Brute Force
i) check if length of two strings is equal or not . If not equal return "No"
ii) create a new string builder and copy given first string to it
iii)initialize i to 0
iv) create another string builder check
v)append characters from index i to length of string to check
vi)append characters from index 0 to i to string builder check
vii)check whether before string builder and current string builder are equal or not
viii) if equal return Yes
ix) increment i value by 1
x)repeat steps iv to ix till i is less than string length
xi)return "No"
Time Complexity: O(n^2)
Space Complexity: O(n)
Approach 2: using indexOf method
i) check if both lengths are equal are not if not return "No"
ii) concatinate the first string twice
iii) check whether the second string exist in the concatinated string using indexOf method
iv) if it exists return "yes" else return "No"
Time Complexity: O(n)
Space Complexity:O(n)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution