Array ( [0] => [1] => questions [2] => Basic [3] => Equal-Strings ) Strings | Equal Strings | THE INQUISITIVE





Equal Strings

LEVEL:Beginner

Description

Given two strings K and L. You have to check if strings are rotations of each other or not.

Input Format

First line contains the string K. Next line contains the string L.

Output Format

Print Yes if they are same else print No.


Example 1:

Input
educate
cateedu
Output
Yes	
Example 2:

Input
never 
everr
Output
No
Example 3:

Input
learn
arnln
Output
Yes

oops

Login to see Discussion




Approach

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.

oops

Login to see Solution