Convert characters of string1 to characters present in string2 by incrementing or decrementing lexicographically
Given two strings A and B having lower case English alphabets, the task is to find the number of operations required to convert string A such that it only contains letters that are also in the string B where at each operation, the current character can be changed to either the next character or the previous character. Also, the next character after ‘z‘ is ‘a‘ and the previous character after ‘a‘ is ‘z‘.
Examples:
Input: A = “abcd”, B = “bc”
Output: 2
Explanation: The given string A = “abcd” can be converted into the string “bbcc” by incrementing the 1st character of the string in 1st move and decrementing the last character in 2nd move. Hence, A = “bbcc” has all the characters that are also in string B. Therefore, the required number of moves is 2 which is the minimum possible.Input: A = “abcde”, B = “yg”
Output: 14
Approach: The given problem can be solved using a greedy approach. The idea is to convert all the characters in string A that are not in the string B into the nearest possible character that exists in string B which can be done by following the below steps:
- Store the frequency of characters of the string B in an unordered map m.
- Iterate through the given string A and check for each character in B, the number of steps required to convert the current character to it.
- The ways to convert a character x to y can be of two different types as follows:
- The 1st one is to increment the character x until y is reached i.e, clockwise rotation.
- The 2nd one is to decrement the character x until y is reached, i.e, anticlockwise rotation.
- Hence, maintain the sum of all the minimum of the moves in clockwise and anticlockwise rotations for each character in A, in a variable ans, which is the required value.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate minimum number // of operations required to convert A // such that it only contains letters // in the string B int minOperations(string a, string b) { // Stores the characters in string B unordered_map< char , int > m; for ( int i = 0; i < b.size(); i++) { m[b[i]]++; } // Stores the min count of operations int ans = 0; // Loop to iterate the given array for ( int i = 0; i < a.size(); i++) { // Stores the minimum number of // moves required for ith index int mn = INT_MAX; // Loop to calculate the number // of moves required to convert // the current index for ( int j = 0; j < 26; j++) { int val = a[i] - 'a' ; // If the current character // is also in b if (m[a[i]] > 0) { mn = 0; break ; } else if (m[ 'a' + j] > 0) { // Minimum of abs(val - j), // clockwise rotation and // anti clockwise rotation mn = min(mn, min( abs (val - j), min((26 - val) + j, val + (26 - j)))); } } // Update answer ans += mn; } // Return Answer return ans; } int main() { string A = "abcde" ; string B = "yg" ; cout << minOperations(A, B); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { // Function to calculate minimum number // of operations required to convert A // such that it only contains letters // in the String B static int minOperations(String a, String b) { // Stores the characters in String B HashMap<Character,Integer> m = new HashMap<Character,Integer>(); for ( int i = 0 ; i < b.length(); i++) { if (m.containsKey(b.charAt(i))){ m.put(b.charAt(i), m.get(b.charAt(i))+ 1 ); } else { m.put(b.charAt(i), 1 ); } } // Stores the min count of operations int ans = 0 ; // Loop to iterate the given array for ( int i = 0 ; i < a.length(); i++) { // Stores the minimum number of // moves required for ith index int mn = Integer.MAX_VALUE; // Loop to calculate the number // of moves required to convert // the current index for ( int j = 0 ; j < 26 ; j++) { int val = a.charAt(i) - 'a' ; // If the current character // is also in b if (m.containsKey(a.charAt(i))&&m.get(a.charAt(i)) > 0 ) { mn = 0 ; break ; } else if (m.containsKey(( char )( 'a' + j))&&m.get(( char )( 'a' + j)) > 0 ) { // Minimum of Math.abs(val - j), // clockwise rotation and // anti clockwise rotation mn = Math.min(mn, Math.min(Math.abs(val - j), Math.min(( 26 - val) + j, val + ( 26 - j)))); } } // Update answer ans += mn; } // Return Answer return ans; } public static void main(String[] args) { String A = "abcde" ; String B = "yg" ; System.out.print(minOperations(A, B)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the above approach INT_MAX = 2147483647 # Function to calculate minimum number # of operations required to convert A # such that it only contains letters # in the string B def minOperations(a, b): # Stores the characters in string B m = {} for i in range ( 0 , len (b)): if b[i] in m: m[b[i]] + = 1 else : m[b[i]] = 1 # Stores the min count of operations ans = 0 # Loop to iterate the given array for i in range ( 0 , len (a)): # Stores the minimum number of # moves required for ith index mn = INT_MAX # Loop to calculate the number # of moves required to convert # the current index for j in range ( 0 , 26 ): val = ord (a[i]) - ord ( 'a' ) # If the current character # is also in b if (a[i] in m and m[a[i]] > 0 ): mn = 0 break elif ( chr ( ord ( 'a' ) + j) in m and m[ chr ( ord ( 'a' ) + j)] > 0 ): # Minimum of abs(val - j), # clockwise rotation and # anti clockwise rotation mn = min (mn, min ( abs (val - j), min (( 26 - val) + j, val + ( 26 - j)))) # Update answer ans + = mn # Return Answer return ans # Driver code if __name__ = = "__main__" : A = "abcde" B = "yg" print (minOperations(A, B)) # This code is contributed by rakeshsahni |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to calculate Minimum number // of operations required to convert A // such that it only contains letters // in the String B static int MinOperations(String a, String b) { // Stores the characters in String B Dictionary< char , int > m = new Dictionary< char , int >(); for ( int i = 0; i < b.Length; i++) { if (m.ContainsKey(b[i])) { m[b[i]] += 1; } else { m[b[i]] = 1; } } // Stores the Min count of operations int ans = 0; // Loop to iterate the given array for ( int i = 0; i < a.Length; i++) { // Stores the Minimum number of // moves required for ith index int mn = int .MaxValue; // Loop to calculate the number // of moves required to convert // the current index for ( int j = 0; j < 26; j++) { int val = a[i] - 'a' ; // If the current character // is also in b if (m.ContainsKey(a[i]) && m[a[i]] > 0) { mn = 0; break ; } else if (m.ContainsKey(( char )( 'a' + j)) && m[( char )( 'a' + j)] > 0) { // Minimum of Math.abs(val - j), // clockwise rotation and // anti clockwise rotation mn = Math.Min(mn, Math.Min(Math.Abs(val - j), Math.Min((26 - val) + j, val + (26 - j)))); } } // Update answer ans += mn; } // Return Answer return ans; } public static void Main() { String A = "abcde" ; String B = "yg" ; Console.Write(MinOperations(A, B)); } } // This code is contributed by gfgking |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to calculate minimum number // of operations required to convert A // such that it only contains letters // in the string B function minOperations(a, b) { // Stores the characters in string B let m = new Map(); for (let i = 0; i < b.length; i++) { if (m.has(b[i])) { m.set(b[i], m.get(b[i]) + 1); } else { m.set(b[i], 1); } } // Stores the min count of operations let ans = 0; // Loop to iterate the given array for (let i = 0; i< a.length; i++) { // Stores the minimum number of // moves required for ith index let mn = 999999; // Loop to calculate the number // of moves required to convert // the current index for (let j = 0; j< 26; j++) { let val = a[i].charCodeAt(0) - 'a' .charCodeAt(0); // If the current character // is also in b if (m.get(a[i]) > 0) { mn = 0; break ; } else if (m.has(String.fromCharCode( 'a' .charCodeAt(0) + j)) && m.get(String.fromCharCode( 'a' .charCodeAt(0) + j)) > 0) { // Minimum of abs(val - j), // clockwise rotation and // anti clockwise rotation mn = Math.min(mn, Math.min(Math.abs(val - j), Math.min((26 - val) + j, val + (26 - j)))); } } // Update answer ans += mn; } // Return Answer return ans; } let A = "abcde" ; let B = "yg" ; document.write(minOperations(A, B)); // This code is contributed by Potta Lokesh </script> |
14
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...