Number of rotations required to make string
Number of rotations required to make string
This is my code to count the number of rotations.
But IDK, What is the problem with it.
Can anyone explain and help me out.
Test Case: Input: david vidda
Output: 2
I tried to have brute force approach but, that wasn't working even.
Can anyone point out my mistake??
import java.util.*;
class solution
public static int arrayLeftRotation(StringBuilder str1, StringBuilder str2)
int i;
int count =0;
for (i = 0; i < str1.length(); i++)
if(str1.equals(str2))
count++;
str1 = leftRotatebyOne(str1);
System.out.println(str1);
else return count;
return count;
static StringBuilder leftRotatebyOne(StringBuilder str)
int i;
char temp = str.charAt(0);
for (i = 0; i < str.length()-1; i++)
str.setCharAt(str.indexOf(str.charAt(i)+""),str.charAt(i+1));
str.setCharAt(i,temp);
return str;
public static void main(String args)
Scanner in = new Scanner(System.in);
String strr1= in.nextLine();
StringBuilder str1 = new StringBuilder(strr1);
String strr2 = in.nextLine();
StringBuilder str2 = new StringBuilder(strr2);
System.out.print(arrayLeftRotation(str1, str2));
if(str1!=str2)
arrayLeftRotation
equals()
@RobertKock Hey, Thanks for replying. But that doesn't work even I tried that also! Please Help! Thanks!
– Shailesh200
Sep 14 '18 at 8:09
Please edit with the corrected code using the equals method
– cricket_007
Sep 14 '18 at 8:14
4 Answers
4
Your method leftRotateByOne
appears more complicated than necessary.
Try this:
leftRotateByOne
public class Solution
public static int arrayLeftRotation(String str1,
String str2)
int nr_rotate;
int counter;
nr_rotate = 0;
for (counter = 0; counter < str1.length(); counter++)
if (str1.equals(str2))
return (nr_rotate);
else
str1 = leftRotateByOne(str1);
nr_rotate++;
System.out.println(str1);
// No possible solution
return (-1);
// arrayLeftRotation
public static String leftRotateByOne(String str)
return (str.substring(1) + str.charAt(0));
public static void main(String args)
String str1 = "david";
String str2 = "vidda";
System.out.print(arrayLeftRotation(str1, str2));
// class Solution
Hey! Thanks For your help! Worked!
– Shailesh200
Sep 14 '18 at 8:30
Another possible solution for arrayLeftRotation
,
arrayLeftRotation
public static int arrayLeftRotation(String str1, String str2)
StringBuilder builder = new StringBuilder(str1);
for (int i = 0; i < str1.length(); i++)
builder.append(str1.charAt(i)).delete(0, 1);
if (str2.equals(builder.toString()))
return i + 1;
return -1;
Note: this will return -1
if no matches found.
-1
Can you explain me the logic behind the line: builder.append(str1.charAt(i)).delete(0, 1);
– Shailesh200
Sep 14 '18 at 13:28
@Shailesh200 builder contains string str1, first, char at position 'i' of str1 appends to end of builder then removes first character of builder until match is found
– benjamin c
Sep 14 '18 at 13:40
The trick is to append the input string to itself, then call String#indexOf
. It will give you the index at which the doubled string contains the expected string, which is what you're looking for.
String#indexOf
Example:
public static int numberOfRotations(String input, String expected)
final String doubledInput = input + input;
return doubledInput.indexOf(expected);
If you really want to implement it yourself, you need to simplify your code to minimize the possibility of making mistakes.
public static String rotate(String input)
return input.substring(1) + input.charAt(0);
public static int numberOfRotations(String input, String expected)
// handle edge cases (null, empty, etc.) here
String rotatedInput = input;
int count = 0;
while (!rotatedInput.equals(expected) && count < input.length())
rotatedInput = rotate(rotatedInput);
count++;
return count == input.length() ? -1 : count;
I am just trying to point out where your error lies and fix it.
Your error lies here in your leftRotatebyOne
:
leftRotatebyOne
for (i = 0; i < str.length()-1; i++)
str.setCharAt(str.indexOf(str.charAt(i)+""),str.charAt(i+1)); // your error while shifting to the left;
What you are trying to do is shifting one position to the left, and you should just do it as:
for (i = 0; i < str.length()-1; i++)
str.setCharAt(i,str.charAt(i+1));
And then your method will work.
But I have to say Alex M has provided a cleaner solution to your problem. Perhaps you should have a try.
Your solution then can be (after the fix):
public class RotationCount
public static int arrayLeftRotation(StringBuilder str1, StringBuilder str2)
int i;
int count = 0;
for (i = 0; i < str1.length(); i++)
if (!str1.toString().equals(str2.toString()))
count++;
str1 = leftRotatebyOne(str1);
else return count;
return count;
static StringBuilder leftRotatebyOne(StringBuilder str)
int i;
char temp = str.charAt(0);
for (i = 0; i < str.length() - 1; i++)
str.setCharAt(i, str.charAt(i + 1));
str.setCharAt(i, temp);
return str;
public static void main(String args)
StringBuilder str1 = new StringBuilder("david");
StringBuilder str2 = new StringBuilder("vidda");
System.out.print(arrayLeftRotation(str1, str2));
Hey, Thanks, Bro! Nice Solution though! Really appreciated!
– Shailesh200
Sep 14 '18 at 13:27
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I didn't verify the algorithm but the statement
if(str1!=str2)
withinarrayLeftRotation
is certainly wrong: useequals()
to compare objects– Robert Kock
Sep 14 '18 at 8:04