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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
void minCost(string s1, string s2)
{
if (s1.length() != s2.length()) {
cout << -1 << endl;
return ;
}
int n = s1.length();
int res = INT_MAX;
for ( char c = 'a' ; c <= 'z' ; c++) {
int ops = 0;
for ( int i = 0; i < n; i++) {
char c1 = s1[i] == '_' ? c : s1[i];
char c2 = s2[i] == '_' ? c : s2[i];
if (c1 == c2)
continue ;
else {
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
if (ops < res) {
res = ops;
}
}
cout << res << endl;
}
int main()
{
string s1 = "g_e_s" , s2 = "ge_ks" ;
minCost(s1, s2);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static boolean isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static void minCost(String s1, String s2)
{
if (s1.length() != s2.length()) {
System.out.println(- 1 );
return ;
}
int n = s1.length();
int res = Integer.MAX_VALUE;
for ( char c = 'a' ; c <= 'z' ; c++) {
int ops = 0 ;
for ( int i = 0 ; i < n; i++) {
char c1 = s1.charAt(i) == '_' ? c : s1.charAt(i);
char c2 = s2.charAt(i) == '_' ? c : s2.charAt(i);
if (c1 == c2)
continue ;
else {
ops
= ops
+ (isVowel(s1.charAt(i)) != isVowel(s2.charAt(i))
? 2
: 1 );
}
}
if (ops < res) {
res = ops;
}
}
System.out.println(res);
}
public static void main(String args[])
{
String s1 = "g_e_s" , s2 = "ge_ks" ;
minCost(s1, s2);
}
}
|
Python3
import sys
def isVowel(c):
return (c = = 'a' or c = = 'e' or c = = 'i'
or c = = 'o' or c = = 'u' )
def minCost(s1, s2):
if ( len (s1) ! = len (s2)):
print ( - 1 )
return
n = len (s1)
res = sys.maxsize
for c in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
ops = 0
for i in range (n):
if s1[i] = = '_' :
c1 = chr (c)
else :
c1 = s1[i]
if s2[i] = = '_' :
c2 = chr (c)
else :
c2 = s2[i]
if (c1 = = c2):
continue
else :
if isVowel(s1[i]) ! = isVowel(s2[i]):
ops + = 2
else :
ops + = 1
if (ops < res):
res = ops
print (res)
if __name__ = = "__main__" :
s1 = "g_e_s"
s2 = "ge_ks"
minCost(s1, s2)
|
C#
using System;
class GFG
{
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static void minCost( string s1, string s2)
{
if (s1.Length != s2.Length) {
Console.WriteLine(-1);
return ;
}
int n = s1.Length;
int res = Int32.MaxValue;
for ( char c = 'a' ; c <= 'z' ; c++) {
int ops = 0;
for ( int i = 0; i < n; i++) {
char c1 = s1[i] == '_' ? c : s1[i];
char c2 = s2[i] == '_' ? c : s2[i];
if (c1 == c2)
continue ;
else {
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
if (ops < res) {
res = ops;
}
}
Console.WriteLine(res);
}
public static void Main()
{
string s1 = "g_e_s" , s2 = "ge_ks" ;
minCost(s1, s2);
}
}
|
Javascript
<script>
function isVowel(c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
function minCost(s1, s2)
{
if (s1.length != s2.length) {
document.write(-1);
return ;
}
let n = s1.length;
let res = Number. MAX_SAFE_INTEGER;
let c = 'a' ;
for (let j = 0; j < 26; j++) {
let ops = 0;
for (let i = 0; i < n; i++) {
let c1 = s1[i] == '_' ? c : s1[i];
let c2 = s2[i] == '_' ? c : s2[i];
if (c1 == c2)
continue ;
else {
ops
= ops
+ (isVowel(s1[i]) != isVowel(s2[i])
? 2
: 1);
}
}
c = String.fromCharCode(c.charCodeAt(0) + 1);
if (ops < res) {
res = ops;
}
}
document.write(res);
}
let s1 = "g_e_s" ;
let s2 = "ge_ks" ;
minCost(s1, s2);
</script>
|
Time Complexity: O(26* N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...