Given strings S and T. The task is to check if S can be converted to T by performing at most K operations. For the ith operation, select any character in S which has not been selected before, and increment the chosen character i times (i.e., replacing it with the letter i times ahead in the alphabet)
Note: The increment is cyclic (i.e., incrementing ‘z’ by 1 makes the character ‘a’)
Examples:
Input: A = “input”, B = “output”, N = 9
Output: True
Explanation: In the 6th operation, we shift ‘i’ 6 times to get ‘o’. And in the 7th operation, we shift ‘n’ to get ‘u’.Input: A = “aab”, B = “bbb”, N = 27
Output: True
Explanation: In the 1st move, we shift the first ‘a’ 1 time to get ‘b’. In the 27th move, we shift the second ‘a’ 27 times to get ‘b’.
An approach using Hashing:
One important thing is to notice that we can only shift a letter once, and we cannot change more than one letter by the same number of shifts (i). In other words, if we shift one letter by 1, no other letters can be shifted by 1. If we need to shift by 1 again, you need to use “wrapping” and shift by 27 (which is 1 + 26).
Follow the steps below to implement the above idea:
- Check if the size of both strings is not equal
- If true, return false
- Initialize an array arr[] of size 26. where, arr[i] = x implies that there are x characters in string A that need an increment of i times to match the characters in string B.
- Iterate over the string A.
- Calculate the increment needed to convert A[i] to B[i]
- Calculate the total operation require to shift all the characters that need the same amount of increment.
- If the total operation required is greater than N, return false
- Increment the frequency of shift in array arr by 1
- Finally, return true
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to check if conversion is possible bool canConvert(string A, string B, int N) { // Check if size of both strings are // not equal If true, return false; if (A.size() != B.size()) return false ; // Initialize an array of size 26 // where, arr[i] = x implies that // there are x characters in string A // that need a shift of i times to // match the characters in string B. vector< int > arr(26, 0); // Iterate over the string A. for ( int i = 0; i < A.size(); i++) { // Calculate the increment needed to // convert A[i] to B[i] int shift = (B[i] - A[i] + 26) % 26; // Calculate the total operations // require to increment all elements // requiring same number of increment. // If total operation required is // greater than N, return false if (shift != 0 && shift + arr[shift] * 26 > N) return false ; // Increment the frequency of // shift in array arr arr[shift]++; } // Finally, return true return true ; } // Driver code int main() { string A = "abcabc" , B; int N = 3; // Function Call if (canConvert(A, B, N)) cout << "True" ; else cout << "False" ; return 0; } |
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to check if conversion is possible public static boolean canConvert(String A, String B, int N) { // Check if size of both strings are // not equal If true, return false; if (A.length() != B.length()) return false ; // Initialize an array of size 26 // where, arr[i] = x implies that // there are x characters in string A // that need a shift of i times to // match the characters in string B. int arr[] = new int [ 26 ]; // Iterate over the string A. for ( int i = 0 ; i < 26 ; i++) { // Calculate the increment needed to // convert A[i] to B[i] int shift = (B.charAt(i) - A.charAt(i) + 26 ) % 26 ; // Calculate the total operations // require to increment all elements // requiring same number of increment. // If total operation required is // greater than N, return false if (shift != 0 && shift + arr[shift] * 26 > N) return false ; // Increment the frequency of // shift in array arr arr[shift]++; } // Finally, return true return true ; } // Driver Code public static void main(String[] args) { String A = "abcabc" ; String B = "" ; int N = 3 ; // Function Call if (canConvert(A, B, N)) System.out.print( "True" ); else System.out.print( "False" ); } } // This code is contributed by Rohit Pradhan |
Python3
# python code to implement the approach # Function to check if conversion is possible def canConvert(A, B, N): # Check if size of both s are # not equal If true, return false if ( len (A) ! = len (B)): return False # ilize an array of size 26 # where, arr[i] = x implies that # there are x characters in A # that need a shift of i times to # match the characters in B. arr = [ 0 ] * 26 # (26, 0) # Iterate over the A. for i in range ( 0 , len (A)): # Calculate the increment needed to # convert A[i] to B[i] shift = ( ord (B[i]) - ord (A[i]) + 26 ) % 26 # Calculate the total operations # require to increment all elements # requiring same number of increment. # If total operation required is # greater than N, return false if (shift ! = 0 and shift + arr[shift] * 26 > N): return False # Increment the frequency of # shift in array arr arr[shift] + = 1 # Finally, return true return True # Driver code A = "abcabc" B = "" N = 3 # Function Call if (canConvert(A, B, N)): print ( "True" ) else : print ( "False" ) # This code is contributed by Pushpesh Raj. |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Function to check if conversion is possible public static bool canConvert( string A, string B, int N) { // Check if size of both strings are // not equal If true, return false; if (A.Length != B.Length) return false ; // Initialize an array of size 26 // where, arr[i] = x implies that // there are x characters in string A // that need a shift of i times to // match the characters in string B. int [] arr = new int [26]; // Iterate over the string A. for ( int i = 0; i < 26; i++) { // Calculate the increment needed to // convert A[i] to B[i] int shift = (B[i] - A[i] + 26) % 26; // Calculate the total operations // require to increment all elements // requiring same number of increment. // If total operation required is // greater than N, return false if (shift != 0 && shift + arr[shift] * 26 > N) return false ; // Increment the frequency of // shift in array arr arr[shift]++; } // Finally, return true return true ; } // Driver Code public static void Main() { string A = "abcabc" ; string B = "" ; int N = 3; // Function Call if (canConvert(A, B, N)) Console.WriteLine( "True" ); else Console.WriteLine( "False" ); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
// JavaScript code for the above approach // Function to check if conversion is possible function canConvert(A, B, N) { // Check if size of both strings are // not equal If true, return false; if (A.length != B.length) return false ; // initialize an array of size 26 // where, arr[i] = x implies that // there are x characters in string A // that need a shift of i times to // match the characters in string B. let arr = new Array(26).fill(0); // Iterate over the string A. for (let i = 0; i < A.length; i++) { // Calculate the increment needed to // convert A[i] to B[i] let shift = (B[i].charCodeAt(0) - A[i].charCodeAt(0) + 26) % 26; // Calculate the total operations // require to increment all elements // requiring same number of increment. // If total operation required is // greater than N, return false if (shift != 0 && shift + arr[shift] * 26 > N) return false ; // Increment the frequency of // shift in array arr arr[shift]++; } // Finally, return true return true ; } // Driver code let A = "abcabc" , B = "" ; let N = 3; // Function Call if (canConvert(A, B, N)) console.log( "True" ); else console.log( "False" ); // This code is contributed by Potta Lokesh |
False
Time Complexity: O(size(A))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!