Check if an integer is rotation of another given integer
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:
- Convert the integers A and B to strings.
- For each digit in string B, remove it from the string and append it to the end of the string.
- Check if the resulting string matches the string representation of A.
- If there is a match, return true.
- If no match is found after all possible rotations have been checked, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int main()
{
int A = 967, B = 679;
if (check(A, B))
cout << "Yes" ;
else
cout << "No" << endl;
return 0;
}
|
Java
public class Main {
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
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
A = 967
B = 679
if check(A, B):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
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 ;
}
public static void Main()
{
int A = 967, B = 679;
if (Check(A, B))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
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 ;
}
let A = 967, B = 679;
if (check(A, B))
console.log( "Yes" );
else
console.log( "No" );
|
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++
#include <bits/stdc++.h>
using namespace std;
int check( int A, int B)
{
if (A == B) {
return 1;
}
int dig1 = floor ( log10 (A) + 1);
int dig2 = floor ( log10 (B) + 1);
if (dig1 != dig2) {
return 0;
}
int temp = A;
while (1) {
int power = pow (10, dig1 - 1);
int firstdigit = A / power;
A = A - firstdigit * power;
A = A * 10 + firstdigit;
if (A == B) {
return 1;
}
if (A == temp) {
return 0;
}
}
}
int main()
{
int A = 967, B = 679;
if (check(A, B))
cout << "Yes" ;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int check( int A, int B)
{
if (A == B) {
return 1 ;
}
int dig1 = ( int )Math.floor(Math.log10(A) + 1 );
int dig2 = ( int )Math.floor(Math.log10(B) + 1 );
if (dig1 != dig2) {
return 0 ;
}
int temp = A;
while ( true ) {
int power = ( int )Math.pow( 10 , dig1 - 1 );
int firstdigit = A / power;
A = A - firstdigit * power;
A = A * 10 + firstdigit;
if (A == B) {
return 1 ;
}
if (A == temp) {
return 0 ;
}
}
}
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" );
}
}
|
Python3
import math
def check(A, B) :
if (A = = B) :
return 1
dig1 = math.floor(math.log10(A) + 1 )
dig2 = math.floor(math.log10(B) + 1 )
if (dig1 ! = dig2) :
return 0
temp = A
while ( True ) :
power = pow ( 10 , dig1 - 1 )
firstdigit = A / / power
A = A - firstdigit * power
A = A * 10 + firstdigit
if (A = = B) :
return 1
if (A = = temp) :
return 0
A, B = 967 , 679
if (check(A, B)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
static int check( int A, int B)
{
if (A == B)
{
return 1;
}
int dig1 = ( int )Math.Floor(Math.Log10(A) + 1);
int dig2 = ( int )Math.Floor(Math.Log10(B) + 1);
if (dig1 != dig2)
{
return 0;
}
int temp = A;
while ( true )
{
int power = ( int )Math.Pow(10, dig1 - 1);
int firstdigit = A / power;
A = A - firstdigit * power;
A = A * 10 + firstdigit;
if (A == B)
{
return 1;
}
if (A == temp)
{
return 0;
}
}
}
public static void Main(String[] args)
{
int A = 967, B = 679;
if (check(A, B) == 1)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check(A, B)
{
if (A == B)
{
return 1;
}
let dig1 = Math.floor(Math.log10(A) + 1);
let dig2 = Math.floor(Math.log10(B) + 1);
if (dig1 != dig2)
{
return 0;
}
let temp = A;
while ( true )
{
let power = Math.pow(10, dig1 - 1);
let firstdigit = parseInt(A / power, 10);
A = A - firstdigit * power;
A = A * 10 + firstdigit;
if (A == B)
{
return 1;
}
if (A == temp)
{
return 0;
}
}
}
let A = 967, B = 679;
if (check(A, B) == 1)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(digit(N))
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...