Sunday, October 28, 2018

Can you write an algorithm to check if a string is rotation of each other or not ?

Problem statement:
write an algorithm to check if a string is a rotation of each other or not!!
  1. public class CheckStringRotation {
  2.     public static void main(String args[]) {
  3.         String str1 = "ishaan";
  4.         String str2 = "aanish";
  5.         String temp = str1.concat(str1);
  6.         if (temp.contains(str2)) {
  7.             System.out.println(str2 + " is a rotation of " + str1);
  8.         } else {
  9.             System.out.println(str2 + " is not a rotation of " + str1);
  10.         }
  11.     }
  12. }
Output:
aanish is the rotation of ishaan

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...