# To determine whether string is rotated or not in java (without using loops)

In this tutorial, we will learn how to determine whether the string is one of its rotation of the given string in Java.

For example, if the given string is ABCD then its circular rotations are BCDA, CDBA, DABC, ABCD. If the input string is present in either of the obtained rotations the output should print true otherwise should print false.

## determine whether string is rotated or not in java

Input Format

The first line of input contains N – length of the string. and the Second line contains another string of length M.

Constraints

```1 <= N <= 100000

1<= M <= 100000```

Output Format

Print ‘true’ if second string is one of the rotations of first string. Otherwise print ‘false’.

Sample Input:

```ABCD

BCDA```

Sample Output:

`true`

Explanation: BCDA is one its rotation of ABCD.

Without using loops we can solve this problem using indexOf() method. First, we concatenate the given string itself and with indexOf() method we can identify at which index the available string is present. If it is true then it is one of its rotations then it is false.

String indexOf(String str): This method returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

```import java.util.*;
class codespeedy
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
String str1="hello";
String str2="llohe";
//Form a new string by concatenating the given string itself
String str=str1+str1; //hellohello
System.out.println("Output:");
if(str1.length()==str2.length() && str.indexOf(str2)!=-1)
{
System.out.println("true");
}
else {
System.out.println("false");
}
}
}```
```Output:
true```

We hope this tutorial helped you to understand how to solve the given problem statement without using loops.

Learn:

Lucky Seven Problem in Java

Find all distinct solutions to N-Queens problem in Java