logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blog
  • Crypto Tools
  • Dev Feed
  • Login
Story
Follow @devglan

Check Given String is Rotation of Another String

author-image   By Dhiraj Ray,   06 March, 2018 5K

Description

Given two string s1 and s2 then write a java program to check if s1 is the rotation of another string s2.Here rotation means, each character of s2 must be the same character of s1 but only thing is that the character in s2 can be present at any random position. Following is the java program to check if a given string is a rotation of another string.

package com.devglan.set1;

import java.util.Arrays;

public class StringRotation {

    public boolean checkStringRotation(String s1,String s2){
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();

        Arrays.sort(c1);
        Arrays.sort(c2);
        boolean rotation = false;
        if(Arrays.equals(c1, c2)){
            System.out.println("s2 is a rotated version of s1");
            rotation = true;
        }else{
            System.out.println("s2 is not rotated version of s1.");
        }
        return rotation;

    }
	
	public static void main(String[] args){
        StringRotation rotation = new StringRotation();
        rotation.checkStringRotation("checkStringRotation", "RotationcheckString");
        //rotation.checkStringRotation_OtherWay("checkStringRotation", "StringRotationcheck");
    }

Arrays.sort() is an implicit method provided by java. Once both the array is sorted and if given string is a rotation of another, then the array must be equal.

Another way to check for string rotation is by using contains() method. Following is the implementation.

 public boolean checkStringRotation_OtherWay(String s1,String s2){
        boolean rotation = false;
        if(s1.length() != s2.length()) {
            System.out.println("s2 is not rotated version of s1");
        } else {
            String s3 = s1 + s1;

            if (s3.contains(s2)) {
                System.out.println("s2 is a rotated version of s1");
                rotation = true;
            } else {
                System.out.println("s2 is not rotated version of s1");
            }
        }
        return rotation;

    }

Explanation

Click to Suggest Your Own Explanation

Arrays.sort() is an implicit method provided by java. Once both the array is sorted and if given string is a rotation of another, then the array must be equal.

Other Similar Java Programs:

1 Java Program to test if given number is Armstrong or not
2 Java Program to test if a given number is Fibonacci or not
3 java program to find distinct word list from a file
4 Java program to find duplicate character from a string
5 Java Program to find middle index of array where both ends sum is equal
6 Java Program to find line with max character length in descending order in Java
7 Java Program to find max two numbers in an array
8 Java program to find max repeated words from a file
9 Java program to find sum of prime numbers
10 Java program to reverse a given number
11 Java program to find permutations of a given string
12 Java program to reverse a given string
13 Java program to find factorial of a given number
14 Java Program for Binary Search
15 Java Program to Add Two 2D Matrix
16 3 Ways to Check if Given Words are Anagram or not
17 Java Program to Find LCM of a Two Given Number
18 Java Program To Check If A Given Number is A Perfect Number
19 Remove Common Characters From Given Strings
20 Find Second Largest Number in Array
21 Java Program To Find the Longest Palindrome Present in a String
22 Java Program to Reverse an Array in Place Without Using Any Second Array
23 Java Program to Print 1 To 10 Without Using Loop
24 Write a Java Program to Compare Files in Java
25 Java Program to Find missing Number in an Array
26 Java Program to Find First non Repeated Character in a String
27 Write a Java Program to Find Union and Intersection of Arrays in Java
28 Writing a Java program to rotate an array by d elements.
29 Write a Java program to rotate a matrix
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2020 Devglan. All rights reserved.