Open In App

Check if an integer is rotation of another given integer

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given two integers A and B, the task is to check if the integer A is rotation of the digits of the integer B or not. If found to be true, then print “Yes”. Otherwise print “No”.

Examples: 

Input: A= 976, B= 679
Output: Yes

Input: A= 974, B= 2345
Output: No

Approach:

An approach to solve this problem would involve generating all possible rotations of the digits of the integer B and checking if any of them match with A.

Here are the steps:

  1. Convert the integers A and B to strings.
  2. For each digit in string B, remove it from the string and append it to the end of the string.
  3. Check if the resulting string matches the string representation of A.
  4. If there is a match, return true.
  5. If no match is found after all possible rotations have been checked, return false.

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the integer
// A is a rotation of the integer B
bool check(int A, int B) {
    string s1 = to_string(A);
    string s2 = to_string(B);
    for (int i = 0; i < s2.length(); i++) {
        char first = s2[0];
        s2.erase(0, 1);
        s2 += first;
        if (s1 == s2) {
            return true;
        }
    }
    return false;
}
 
 
// Driver Code
int main()
{
    int A = 967, B = 679;
 
    if (check(A, B))
        cout << "Yes";
    else
        cout << "No" << endl;
    return 0;
}


Java




public class Main {
    // Function to check if the integer A is a rotation of
    // the integer B
    static boolean check(int A, int B)
    {
        String s1 = Integer.toString(A);
        String s2 = Integer.toString(B);
        for (int i = 0; i < s2.length(); i++) {
            char first = s2.charAt(0);
            s2 = s2.substring(1) + first;
            if (s1.equals(s2)) {
                return true;
            }
        }
        return false;
    }
 
    public static void main(String[] args)
    {
        int A = 967, B = 679;
 
        if (check(A, B))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3




# Function to check if the integer A is a rotation of the integer B
def check(A, B):
    s1 = str(A)
    s2 = str(B)
    for i in range(len(s2)):
        first = s2[0]
        s2 = s2[1:]
        s2 += first
        if s1 == s2:
            return True
    return False
 
 
# Driver Code
A = 967
B = 679
if check(A, B):
    print("Yes")
else:
    print("No")


C#




using System;
 
public class GFG {
    // Function to check if the integer A is a rotation of
    // the integer B
    static bool Check(int A, int B)
    {
        string s1 = A.ToString();
        string s2 = B.ToString();
 
        for (int i = 0; i < s2.Length; i++) {
            char first = s2[0];
            s2 = s2.Substring(1) + first;
 
            if (s1 == s2) {
                return true;
            }
        }
 
        return false;
    }
 
    // Driver Code
    public static void Main()
    {
        int A = 967, B = 679;
 
        if (Check(A, B))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}


Javascript




// Javascript implementation of the approach
function check(A, B) {
    let s1 = A.toString();
    let s2 = B.toString();
    for (let i = 0; i < s2.length; i++) {
        let first = s2[0];
        s2 = s2.substring(1) + first;
        if (s1 === s2) {
            return true;
        }
    }
    return false;
}
 
// Driver Code
let A = 967, B = 679;
 
if (check(A, B))
    console.log("Yes");
else
    console.log("No");


Output

Yes






Time Complexity:  O((log A + log B) * log B)
       Converting integers A and B to strings takes O(log A + log B) time, where log is the base 10 logarithm.
       The for loop iterates s2.length() times, which is O(log B) in the worst case.
       Within the for loop, the erase, +=, and == operations take O(log B) time in total.
       Therefore, the overall time complexity of the function is O((log A + log B) * log B).

Space Complexity: O(log A + log B), Converting integers A and B to strings requires O(log A + log B) space.

Approach: Follow the steps below to solve the problem:

  • If A == B, then print “Yes”.
  • Calculate the count of digits of integer present in the integer A and B in variables, say dig1 and dig2.
  • If dig1 != dig2 is found not be true, then print “No”.
  • Initialize a variable, say temp. Assign temp = A.
  • Now, iterate and perform the following operations:

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the integer
// A is a rotation of the integer B
int check(int A, int B)
{
 
    if (A == B) {
        return 1;
    }
 
    // Stores the count of digits in A
    int dig1 = floor(log10(A) + 1);
 
    // Stores the count of digits in B
    int dig2 = floor(log10(B) + 1);
 
    // If dig1 not equal to dig2
    if (dig1 != dig2) {
        return 0;
    }
 
    int temp = A;
 
    while (1) {
 
        // Stores position of first digit
        int power = pow(10, dig1 - 1);
 
        // Stores the first digit
        int firstdigit = A / power;
 
        // Rotate the digits of the integer
        A = A - firstdigit * power;
        A = A * 10 + firstdigit;
 
        // If A is equal to B
        if (A == B) {
            return 1;
        }
        // If A is equal to the initial
// value of integer A
        if (A == temp) {
            return 0;
        }
    }
}
 
// Driver Code
int main()
{
    int A = 967, B = 679;
 
    if (check(A, B))
        cout << "Yes";
    else
        cout << "No" << endl;
    return 0;
}


Java




// Java implementation of the approach
import java.io.*;
class GFG {
 
    // Function to check if the integer
    // A is a rotation of the integer B
    static int check(int A, int B)
    {
 
        if (A == B) {
            return 1;
        }
 
        // Stores the count of digits in A
        int dig1 = (int)Math.floor(Math.log10(A) + 1);
 
        // Stores the count of digits in B
        int dig2 = (int)Math.floor(Math.log10(B) + 1);
 
        // If dig1 not equal to dig2
        if (dig1 != dig2) {
            return 0;
        }
 
        int temp = A;
 
        while (true) {
 
            // Stores position of first digit
            int power = (int)Math.pow(10, dig1 - 1);
 
            // Stores the first digit
            int firstdigit = A / power;
 
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
 
            // If A is equal to B
            if (A == B) {
                return 1;
            }
            // If A is equal to the initial
            // value of integer A
            if (A == temp) {
                return 0;
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 967, B = 679;
 
        if (check(A, B) == 1)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Dharanendra L V.


Python3




# Python3 implementation of the approach
import math
 
# Function to check if the integer
# A is a rotation of the integer B
def check(A, B) :
  
    if (A == B) :
        return 1
  
    # Stores the count of digits in A
    dig1 = math.floor(math.log10(A) + 1)
  
    # Stores the count of digits in B
    dig2 = math.floor(math.log10(B) + 1)
  
    # If dig1 not equal to dig2
    if (dig1 != dig2) :
        return 0
  
    temp = A
  
    while (True) :
  
        # Stores position of first digit
        power = pow(10, dig1 - 1)
  
        # Stores the first digit
        firstdigit = A // power
  
        # Rotate the digits of the integer
        A = A - firstdigit * power
        A = A * 10 + firstdigit
  
        # If A is equal to B
        if (A == B) :
            return 1
         
        # If A is equal to the initial value of integer A
        if (A == temp) :
            return 0
           
          # Driver code
A, B = 967, 679
  
if (check(A, B)) :
    print("Yes")
else :
    print("No")
     
    # This code is contributed by divyesh072019.


C#




// C# implementation of the approach
using System;
public class GFG
{
 
    // Function to check if the integer
    // A is a rotation of the integer B
    static int check(int A, int B)
    {
        if (A == B)
        {
            return 1;
        }
 
        // Stores the count of digits in A
        int dig1 = (int)Math.Floor(Math.Log10(A) + 1);
 
        // Stores the count of digits in B
        int dig2 = (int)Math.Floor(Math.Log10(B) + 1);
 
        // If dig1 not equal to dig2
        if (dig1 != dig2)
        {
            return 0;
        }
        int temp = A;
        while (true)
        {
 
            // Stores position of first digit
            int power = (int)Math.Pow(10, dig1 - 1);
 
            // Stores the first digit
            int firstdigit = A / power;
 
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
 
            // If A is equal to B
            if (A == B)
            {
                return 1;
            }
           
            // If A is equal to the initial
            // value of integer A
            if (A == temp)
            {
                return 0;
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int A = 967, B = 679;
        if (check(A, B) == 1)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
    // Javascript implementation of the approach
     
    // Function to check if the integer
    // A is a rotation of the integer B
    function check(A, B)
    {
        if (A == B)
        {
            return 1;
        }
  
        // Stores the count of digits in A
        let dig1 = Math.floor(Math.log10(A) + 1);
  
        // Stores the count of digits in B
        let dig2 = Math.floor(Math.log10(B) + 1);
  
        // If dig1 not equal to dig2
        if (dig1 != dig2)
        {
            return 0;
        }
        let temp = A;
        while (true)
        {
  
            // Stores position of first digit
            let power = Math.pow(10, dig1 - 1);
  
            // Stores the first digit
            let firstdigit = parseInt(A / power, 10);
  
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
  
            // If A is equal to B
            if (A == B)
            {
                return 1;
            }
            
            // If A is equal to the initial
            // value of integer A
            if (A == temp)
            {
                return 0;
            }
        }
    }
     
    let A = 967, B = 679;
    if (check(A, B) == 1)
      document.write("Yes");
    else
      document.write("No");
   
  // This code is contributed by suresh07.
</script>


Output

Yes






 Time Complexity: O(digit(N))
Auxiliary Space: O(1) 



Last Updated : 18 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads