Interview Programs

String Rotation Program

3 Aug , 2015  

Introduction:
Hello Friends, String Rotation Program question is asked for fresher as well as experienced in technical interview or technical aptitude test.
String Rotation Program is one of the favorite question of 3DPLM software, Amazon and many other.
lots of company asked String Rotation Program. String Rotation Program means to check the given two strings are rotationally equal or not.

Coffee Vendor Machine Progrm In Java Click Here

Question:
Write a program to check the given two strings are rotationally equal or not?
e.g using isRotational(String original,String rotated) it will return boolean true or false.

Explanation Of Question In Detail:
First understand what is String rotation?
String rotation means if you have a two Strings one is “conceptlearningcentre” and another one is “learningcentreconcept” observe the two strings carefully, length of the two strings are equal and also second string is subclass of the result of first string concatenated with first strings.

if you are confusing the above statement then let us see this example. concatenate first string with itself “conceptlearningcentreconceptlearningcentre” and then check whether the second string is subset of concatenated string “concept’learningcentreconcept‘learningcentre” using substring method of String class, Then second string is rotationally equal to first one

Sample Input And Output:
Sample Input 1:

Observe the above two string carefully. string2 is rotationally equal to string1 because length of the two strings are same and string2 is a substring of string string1 concatenated with string1. hence output becomes true for this.

Sample Input 2:

Observe the above two string carefully. string2 is not rotationally equal to string1 because length of the two strings are same but string2 is not a substring of string string1 concatenated with string1. hence output becomes false for this input.

Solution 1:

Code Explanation:
First make sure string original and string rotated are of the same length. Then check to see if string rotated is a substring of string original concatenated with original.
Solution for String Rotation Program in java is very simple. First find out the length of two string. if the length of the two strings are not equal then return false otherwise check rotated strings are present in result of original string concatenated with original string or not. if present then returns true else false

Output For Sample Input 1:
after executing the above String Rotation Program see the final output

Output For Sample Input 2:

If you have any doubts , any question regarding this problem comments here. You can also share this problem on social sites by clicking on below links.
If you have any query related to interview program or question in java then mail me you query in detail at hr@conceptlearningcentre.com

Fizz Buzz progrm in java click here

,


One Response

  1. Pravin says:

    i have face same question in 3DPLM software, nice explanation

Leave a Reply

Your email address will not be published. Required fields are marked *