27 Apr 2014

Rotated Strings

Problem Statement:
     Check if the given strings are rotated form of each other.



Check if S2 is the rotated version of S1

Solution:
Run Source Code

The approach is to check if the strings are of same length, then check if s2 is sub-string of s1 concatenated with itself.

Implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/**
 * Check if Given strings are roated form of each other
 * @author PRATEEK
 */
public class RotatedStrings {

 public static void main(String[] args) {
  System.out.println(isRotated("coding","dingco"));
 }
 
 /**
  * @return: true if strings are rotated form of each other
  */
 public static boolean isRotated(String s1,String s2){
  return s1.length()==s2.length() && (s1+s1).indexOf(s2)!=-1 ;
 }
}


Please comment and post your suggestions.
Happy Coding !! :)

No comments:

Post a Comment