Minimum cost to convert a string to another by replacing blanks
Last Updated :
15 Feb, 2022
Given two strings s1 and s2 with lower-case alphabets having length N. The strings s1 and s2 initially may contain some blanks, the task is to find minimum operations to convert a string s1 to s2.
- Initially, if there are any blanks they should be replaced by any same character which cost 0 and
- Any vowel in the string can be replaced by any consonant and any consonant can be replaced by any vowel which costs 1.
Examples:
Input: s1 = "g_e_s", s2 = "ge_ks"
Output: 1
Explanation: Replacing blanks with 'e', the strings become s1= "geees", s2 = "geeks"
In the 3rd index of s1 convert e -> k which costs only 1 operation.
Input: s1 = "abcd", s2 = "aeca"
Output: 2
Approach: Since there are only 26 lower case characters if there are blanks in the strings the blanks can be replaced by each of these characters and minimum cost can be counted to convert string s1 to s2. If both the characters of the string one is a vowel and the other is consonant or vice versa it costs only one unit to transform one character. If both the characters are vowels or consonants and are not equal then it bears cost of 2; consonant -> vowel -> consonant (cost = 2) or vowel -> consonant -> vowel (cost = 2).
Follow these steps to solve the above problem:
- If both the lengths of the strings are not equal return -1.
- Initialize n with the length and res as INT_MAX.
- Now iterate through each of the 26 characters.
- Initialize the variable ops = 0 to store the costs required.
- Traverse from the range [0, n) and check if there is a blank in any of the strings.
- If there is a blank initialize the chars c1 and c2 to store the modified characters.
- If both the chars c1 == c2 (the character after replacing the blank) no operations are required.
- Else if both are vowels or consonants it requires 2 operations else it requires only 1 operation add it to the ops variable.
- After the traversal store the minimum operations in the res (min(ops, res)).
- Print the result res.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether
// a character is vowel or not
bool isVowel(char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u');
}
// Function to calculate minimum cost
void minCost(string s1, string s2)
{
// If both the lengths are not equal
if (s1.length() != s2.length()) {
cout << -1 << endl;
return;
}
int n = s1.length();
// Initialize res with max value
int res = INT_MAX;
// Iterate through every character
// and check the minimum cost by
// replacing the blank by all letters
for (char c = 'a'; c <= 'z'; c++) {
// Initialize ops to check
// the cost required by replacing
// each char c
int ops = 0;
for (int i = 0; i < n; i++) {
// If it is blank replace with c
char c1 = s1[i] == '_' ? c : s1[i];
char c2 = s2[i] == '_' ? c : s2[i];
// If both are equal no ops required
if (c1 == c2)
continue;
else {
// If both are vowels or consonants
// it requires cost as two
// vowel->consonant ->vowel
// and vice versa
// Else 1 operation
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
// Take the minimum
if (ops < res) {
res = ops;
}
}
// Print the result
cout << res << endl;
}
// Driver code
int main()
{
// Initialize the strings
string s1 = "g_e_s", s2 = "ge_ks";
// Function call
minCost(s1, s2);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to check whether
// a character is vowel or not
static boolean isVowel(char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u');
}
// Function to calculate minimum cost
static void minCost(String s1, String s2)
{
// If both the lengths are not equal
if (s1.length() != s2.length()) {
System.out.println(-1);
return;
}
int n = s1.length();
// Initialize res with max value
int res = Integer.MAX_VALUE;
// Iterate through every character
// and check the minimum cost by
// replacing the blank by all letters
for (char c = 'a'; c <= 'z'; c++) {
// Initialize ops to check
// the cost required by replacing
// each char c
int ops = 0;
for (int i = 0; i < n; i++) {
// If it is blank replace with c
char c1 = s1.charAt(i) == '_' ? c : s1.charAt(i);
char c2 = s2.charAt(i) == '_' ? c : s2.charAt(i);
// If both are equal no ops required
if (c1 == c2)
continue;
else {
// If both are vowels or consonants
// it requires cost as two
// vowel->consonant ->vowel
// and vice versa
// Else 1 operation
ops
= ops
+ (isVowel(s1.charAt(i)) != isVowel(s2.charAt(i))
? 2
: 1);
}
}
// Take the minimum
if (ops < res) {
res = ops;
}
}
// Print the result
System.out.println(res);
}
// Driver code
public static void main(String args[])
{
// Initialize the strings
String s1 = "g_e_s", s2 = "ge_ks";
// Function call
minCost(s1, s2);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python 3 program for the above approach
import sys
# Function to check whether
# a character is vowel or not
def isVowel(c):
return (c == 'a' or c == 'e' or c == 'i'
or c == 'o' or c == 'u')
# Function to calculate minimum cost
def minCost(s1, s2):
# If both the lengths are not equal
if (len(s1) != len(s2)):
print(-1)
return
n = len(s1)
# Initialize res with max value
res = sys.maxsize
# Iterate through every character
# and check the minimum cost by
# replacing the blank by all letters
for c in range(ord('a'), ord('z')+1):
# Initialize ops to check
# the cost required by replacing
# each char c
ops = 0
for i in range(n):
# If it is blank replace with c
if s1[i] == '_':
c1 = chr(c)
else:
c1 = s1[i]
if s2[i] == '_':
c2 = chr(c)
else:
c2 = s2[i]
# If both are equal no ops required
if (c1 == c2):
continue
else:
# If both are vowels or consonants
# it requires cost as two
# vowel->consonant ->vowel
# and vice versa
# Else 1 operation
if isVowel(s1[i]) != isVowel(s2[i]):
ops += 2
else:
ops += 1
# Take the minimum
if (ops < res):
res = ops
# Print the result
print(res)
# Driver code
if __name__ == "__main__":
# Initialize the strings
s1 = "g_e_s"
s2 = "ge_ks"
# Function call
minCost(s1, s2)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check whether
// a character is vowel or not
static bool isVowel(char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u');
}
// Function to calculate minimum cost
static void minCost(string s1, string s2)
{
// If both the lengths are not equal
if (s1.Length != s2.Length) {
Console.WriteLine(-1);
return;
}
int n = s1.Length;
// Initialize res with max value
int res = Int32.MaxValue;
// Iterate through every character
// and check the minimum cost by
// replacing the blank by all letters
for (char c = 'a'; c <= 'z'; c++) {
// Initialize ops to check
// the cost required by replacing
// each char c
int ops = 0;
for (int i = 0; i < n; i++) {
// If it is blank replace with c
char c1 = s1[i] == '_' ? c : s1[i];
char c2 = s2[i] == '_' ? c : s2[i];
// If both are equal no ops required
if (c1 == c2)
continue;
else {
// If both are vowels or consonants
// it requires cost as two
// vowel->consonant ->vowel
// and vice versa
// Else 1 operation
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
// Take the minimum
if (ops < res) {
res = ops;
}
}
// Print the result
Console.WriteLine(res);
}
// Driver code
public static void Main()
{
// Initialize the strings
string s1 = "g_e_s", s2 = "ge_ks";
// Function call
minCost(s1, s2);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// Javascript program for the above approach
// Function to check whether
// a character is vowel or not
function isVowel(c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u');
}
// Function to calculate minimum cost
function minCost(s1, s2)
{
// If both the lengths are not equal
if (s1.length != s2.length) {
document.write(-1);
return;
}
let n = s1.length;
// Initialize res with max value
let res = Number. MAX_SAFE_INTEGER;
// Iterate through every character
// and check the minimum cost by
// replacing the blank by all letters
let c = 'a';
for (let j = 0; j < 26; j++) {
// Initialize ops to check
// the cost required by replacing
// each char c
let ops = 0;
for (let i = 0; i < n; i++) {
// If it is blank replace with c
let c1 = s1[i] == '_' ? c : s1[i];
let c2 = s2[i] == '_' ? c : s2[i];
// If both are equal no ops required
if (c1 == c2)
continue;
else {
// If both are vowels or consonants
// it requires cost as two
// vowel->consonant ->vowel
// and vice versa
// Else 1 operation
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
c = String.fromCharCode(c.charCodeAt(0) + 1);
// Take the minimum
if (ops < res) {
res = ops;
}
}
// Print the result
document.write(res);
}
// Driver code
// Initialize the strings
let s1 = "g_e_s";
let s2 = "ge_ks";
// Function call
minCost(s1, s2);
// This code is contributed by Samim Hossain Mondal.
</script>
Time Complexity: O(26* N)
Space Complexity: O(1)