Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times.
Input: S1 = “abcd”, S2 = “dddd”, X = 3
Output: Yes
Explanation:
Given string S1 can be converted to string S2 as-
Character “a” – Shift 3 times – “d”
Character “b” – Shift 2 times – “d”
Character “c” – Shift 1 times – “d”
Character “d” – Shift 0 times – “d”
Input: S1 = “you”, S2 = “ara”, X = 6
Output: Yes
Explanation:
Given string S1 can be converted to string S2 as –
Character “y” – Circular Shift 2 times – “a”
Character “o” – Shift 3 times – “r”
Character “u” – Circular Shift 6 times – “a”
Approach: The idea is to traverse the string and for each index and find the difference between the ASCII values of the character at the respective indices of the two strings. If the difference is less than 0, then for a circular shift, add 26 to get the actual difference. If for any index, the difference exceeds X, then S2 can’t be formed from S1, otherwise possible.
Follow the below steps to implement the above idea:
- Declare integer variables diff and n.
- Assign the length of string s1 to variable n.
- Iterate over each character of the strings.
- If the characters at the current index are the same, continue to the next iteration of the loop.
- Calculate the difference between the ASCII values of the characters at the current index of both strings.
- To handle the circular shift, add 26 to the difference and then take the result modulo 26.
- Check if the resulting difference is greater than x. If yes, output “NO” and return from the function.
- If the loop completes without returning, output “YES” to indicate that the conversion is possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isConversionPossible(string s1,
string s2, int x)
{
int diff, n;
n = s1.length();
for ( int i = 0; i < n; i++) {
if (s1[i] == s2[i])
continue ;
diff = ( int (s2[i] - s1[i])
+ 26)
% 26;
if (diff > x) {
cout << "NO" << endl;
return ;
}
}
cout << "YES" << endl;
}
int main()
{
string s1 = "you" ;
string s2 = "ara" ;
int x = 6;
isConversionPossible(s1, s2, x);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void isConversionPossible(String s1,
String s2,
int x)
{
int diff = 0 , n;
n = s1.length();
for ( int i = 0 ; i < n; i++)
{
if (s1.charAt(i) == s2.charAt(i))
continue ;
diff = (( int )(s2.charAt(i) -
s1.charAt(i)) + 26 ) % 26 ;
if (diff > x)
{
System.out.println( "NO" );
return ;
}
}
System.out.println( "YES" );
}
public static void main (String[] args)
{
String s1 = "you" ;
String s2 = "ara" ;
int x = 6 ;
isConversionPossible(s1, s2, x);
}
}
|
Python3
def isConversionPossible(s1, s2, x):
n = len (s1)
s1 = list (s1)
s2 = list (s2)
for i in range (n):
diff = ord (s2[i]) - ord (s1[i])
if diff = = 0 :
continue
if diff < 0 :
diff = diff + 26
if diff > x:
return False
return True
if __name__ = = "__main__" :
s1 = "you"
s2 = "ara"
x = 6
result = isConversionPossible(s1, s2, x)
if result:
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static void isConversionPossible(String s1,
String s2,
int x)
{
int diff = 0, n;
n = s1.Length;
for ( int i = 0; i < n; i++)
{
if (s1[i] == s2[i])
continue ;
diff = (( int )(s2[i] -
s1[i]) + 26) % 26;
if (diff > x)
{
Console.Write( "NO" );
return ;
}
}
Console.Write( "YES" );
}
public static void Main ()
{
String s1 = "you" ;
String s2 = "ara" ;
int x = 6;
isConversionPossible(s1, s2, x);
}
}
|
Javascript
<script>
function isConversionPossible(s1,s2,x)
{
let diff = 0, n;
n = s1.length;
for (let i = 0; i < n; i++)
{
if (s1[i] == s2[i])
continue ;
diff = ((s2[i].charCodeAt(0) -
s1[i].charCodeAt(0)) + 26) % 26;
if (diff > x)
{
document.write( "NO<br>" );
return ;
}
}
document.write( "YES<br>" );
}
let s1 = "you" ;
let s2 = "ara" ;
let x = 6;
isConversionPossible(s1, s2, x);
</script>
|
Time Complexity:O(N),N=Length(S1)
Auxiliary Space:O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Mar, 2023
Like Article
Save Article