Check if string S can be converted to T by incrementing characters
Last Updated :
16 Dec, 2022
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
- 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++
#include <bits/stdc++.h>
using namespace std;
bool canConvert(string A, string B, int N)
{
if (A.size() != B.size())
return false ;
vector< int > arr(26, 0);
for ( int i = 0; i < A.size(); i++) {
int shift = (B[i] - A[i] + 26) % 26;
if (shift != 0 && shift + arr[shift] * 26 > N)
return false ;
arr[shift]++;
}
return true ;
}
int main()
{
string A = "abcabc" , B;
int N = 3;
if (canConvert(A, B, N))
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static boolean canConvert(String A, String B,
int N)
{
if (A.length() != B.length())
return false ;
int arr[] = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++) {
int shift
= (B.charAt(i) - A.charAt(i) + 26 ) % 26 ;
if (shift != 0 && shift + arr[shift] * 26 > N)
return false ;
arr[shift]++;
}
return true ;
}
public static void main(String[] args)
{
String A = "abcabc" ;
String B = "" ;
int N = 3 ;
if (canConvert(A, B, N))
System.out.print( "True" );
else
System.out.print( "False" );
}
}
|
Python3
def canConvert(A, B, N):
if ( len (A) ! = len (B)):
return False
arr = [ 0 ] * 26
for i in range ( 0 , len (A)):
shift = ( ord (B[i]) - ord (A[i]) + 26 ) % 26
if (shift ! = 0 and shift + arr[shift] * 26 > N):
return False
arr[shift] + = 1
return True
A = "abcabc"
B = ""
N = 3
if (canConvert(A, B, N)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static bool canConvert( string A, string B, int N)
{
if (A.Length != B.Length)
return false ;
int [] arr = new int [26];
for ( int i = 0; i < 26; i++) {
int shift = (B[i] - A[i] + 26) % 26;
if (shift != 0 && shift + arr[shift] * 26 > N)
return false ;
arr[shift]++;
}
return true ;
}
public static void Main()
{
string A = "abcabc" ;
string B = "" ;
int N = 3;
if (canConvert(A, B, N))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
Javascript
function canConvert(A, B, N)
{
if (A.length != B.length)
return false ;
let arr = new Array(26).fill(0);
for (let i = 0; i < A.length; i++) {
let shift = (B[i].charCodeAt(0) - A[i].charCodeAt(0) + 26) % 26;
if (shift != 0 && shift + arr[shift] * 26 > N)
return false ;
arr[shift]++;
}
return true ;
}
let A = "abcabc" , B = "" ;
let N = 3;
if (canConvert(A, B, N))
console.log( "True" );
else
console.log( "False" );
|
Time Complexity: O(size(A))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...