Convert given string to another by minimum replacements of subsequences by its smallest character
Last Updated :
07 Aug, 2023
Given two strings A and B, the task is to count the minimum number of operations required to convert the string A to B. In one operation, select a subsequence from string A and convert every character of that subsequence to the smallest character present in it. If it is not possible to transform, then print "-1".
Examples:
Input: A = "abcab", B = "aabab"
Output: 2
Explanation:
Operation 1: Replacing characters from indices {2, 1} by the smallest character from those indices(i.e. 'b'), transforms A to "abbab".
Operation 2: Replacing characters from indices {1, 0}, by the smallest character from those indices(i.e. 'a'), transforms A to "aabab".
Therefore, the count of operations required to convert string A to B is 2.
Input: A = "aaa", B = "aab"
Output: -1
Explanation:
There is no possible way to convert A to B as string A doesn't contain 'b'.
Approach: The approach is based on the idea that if any character at index i of string A is less than the character at index i of string B, then it is impossible to change A to B because changing a character to a character smaller than itself is not allowed.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the minimum number
// of operation
void transformString(string str1,
string str2)
{
// Storing data
int N = str1.length();
vector<int> convChar[26];
vector<int> str1array[26];
// Initialize both arrays
for (int i = 0; i < 26; i++) {
vector<int> v;
convChar[i] = v;
str1array[i] = v;
}
// Stores the index of character
map<int, char> convertMap;
// Filling str1array, convChar
// and hashmap convertMap.
for (int i = 0; i < N; i++) {
str1array[str1[i] - 'a'].push_back(i);
}
for (int i = 0; i < N; i++) {
// Not possible to convert
if (str1[i] < str2[i]) {
cout << -1 << endl;
return;
}
else if (str1[i] == str2[i])
continue;
else {
convChar[str2[i] - 'a'].push_back(i);
convertMap[i] = str2[i];
}
}
// Calculate result
// Initializing return values
int ret = 0;
vector<vector<int> > retv;
// Iterating the character from
// the end
for (int i = 25; i >= 0; i--) {
vector<int> v = convChar[i];
if (v.size() == 0)
continue;
// Increment the number of
// operations
ret++;
vector<int> v1 = str1array[i];
// Not possible to convert
if (v1.size() == 0) {
cout << -1 << endl;
return;
}
// to check whether the final
// element has been added
// in set S or not.
bool isScompleted = false;
for (int j = 0; j < v1.size(); j++) {
// Check if v1[j] is present
// in hashmap or not
if (convertMap.find(v1[j])
!= convertMap.end()) {
char a = convertMap[v1[j]];
// Already converted then
// then continue
if (a > i + 'a')
continue;
else {
v.push_back(v1[j]);
isScompleted = true;
retv.push_back(v);
break;
}
}
else {
v.push_back(v1[j]);
isScompleted = true;
retv.push_back(v);
break;
}
}
// Not possible to convert
if (!isScompleted) {
cout << -1 << endl;
return;
}
}
// Print the result
cout << ret << endl;
}
// Driver Code
int main()
{
// Given strings
string A = "abcab";
string B = "aabab";
// Function Call
transformString(A, B);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to return the minimum number
// of operation
static void transformString(String str1,
String str2)
{
// Storing data
int N = str1.length();
ArrayList<
ArrayList<Integer>> convChar = new ArrayList<>();
ArrayList<
ArrayList<Integer>> str1array = new ArrayList<>();
// Initialize both arrays
for(int i = 0; i < 26; i++)
{
convChar.add(new ArrayList<>());
str1array.add(new ArrayList<>());
}
// Stores the index of character
Map<Integer,
Character> convertMap = new HashMap<>();
// Filling str1array, convChar
// and hashmap convertMap.
for(int i = 0; i < N; i++)
{
str1array.get(str1.charAt(i) - 'a').add(i);
}
for(int i = 0; i < N; i++)
{
// Not possible to convert
if (str1.charAt(i) < str2.charAt(i))
{
System.out.println(-1);
return;
}
else if (str1.charAt(i) == str2.charAt(i))
continue;
else
{
convChar.get(str2.charAt(i) - 'a').add(i);
convertMap.put(i,str2.charAt(i));
}
}
// Calculate result
// Initializing return values
int ret = 0;
ArrayList<
ArrayList<Integer>> retv = new ArrayList<>();
// Iterating the character from
// the end
for(int i = 25; i >= 0; i--)
{
ArrayList<Integer> v = convChar.get(i);
if (v.size() == 0)
continue;
// Increment the number of
// operations
ret++;
ArrayList<Integer> v1 = str1array.get(i);
// Not possible to convert
if (v1.size() == 0)
{
System.out.println(-1);
return;
}
// To check whether the final
// element has been added
// in set S or not.
boolean isScompleted = false;
for(int j = 0; j < v1.size(); j++)
{
// Check if v1[j] is present
// in hashmap or not
if (convertMap.containsKey(v1.get(j)))
{
char a = convertMap.get(v1.get(j));
// Already converted then
// then continue
if (a > i + 'a')
continue;
else
{
v.add(v1.get(j));
isScompleted = true;
retv.add(v);
break;
}
}
else
{
v.add(v1.get(j));
isScompleted = true;
retv.add(v);
break;
}
}
// Not possible to convert
if (!isScompleted)
{
System.out.println(-1);
return;
}
}
// Print the result
System.out.println(ret);
}
// Driver Code
public static void main (String[] args)
{
// Given strings
String A = "abcab";
String B = "aabab";
// Function call
transformString(A, B);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to return the minimum number
# of operation
def transformString(str1, str2):
# Storing data
N = len(str1)
convChar = []
str1array = []
# Initialize both arrays
for i in range(26):
convChar.append([])
str1array.append([])
# Stores the index of character
convertMap = {}
# Filling str1array, convChar
# and hashmap convertMap.
for i in range(N):
str1array[ord(str1[i]) -
ord('a')].append(i)
for i in range(N):
# Not possible to convert
if (str1[i] < str2[i]):
print(-1)
return
elif (str1[i] == str2[i]):
continue
else:
convChar[ord(str2[i]) -
ord('a')].append(i)
convertMap[i] = str2[i]
# Calculate result
# Initializing return values
ret = 0
retv = []
# Iterating the character from
# the end
for i in range(25, -1, -1):
v = convChar[i]
if (len(v) == 0):
continue
# Increment the number of
# operations
ret += 1;
v1 = str1array[i]
# Not possible to convert
if (len(v1) == 0):
print(-1)
return
# To check whether the final
# element has been added
# in set S or not.
isScompleted = False
for j in range(len(v1)):
# Check if v1[j] is present
# in hashmap or not
if (v1[j] in convertMap):
a = v1[j]
# Already converted then
# then continue
if (a > i + ord('a')):
continue
else:
v.append(v1[j])
isScompleted = True
retv.append(v)
break
else:
v.append(v1[j])
isScompleted = True
retv.append(v)
break
# Not possible to convert
if (isScompleted == False):
print(-1)
return
# Print the result
print(ret)
# Driver Code
A = "abcab"
B = "aabab"
# Function call
transformString(A, B)
# This code is contributed by dadi madhav
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the minimum number
// of operation
static void transformString(string str1, string str2)
{
// Storing data
int N = str1.Length;
List<List<int>> convChar = new List<List<int>>();
List<List<int>> str1array = new List<List<int>>();
// Initialize both arrays
for(int i = 0; i < 26; i++)
{
convChar.Add(new List<int>());
str1array.Add(new List<int>());
}
// Stores the index of character
Dictionary<int,
char> convertMap = new Dictionary<int,
char>();
// Filling str1array, convChar
// and hashmap convertMap.
for(int i = 0; i < N; i++)
{
str1array[str1[i] - 'a'].Add(i);
}
for(int i = 0; i < N; i++)
{
// Not possible to convert
if (str1[i] < str2[i])
{
Console.WriteLine(-1);
return;
}
else if (str1[i] == str2[i])
continue;
else
{
convChar[str2[i] - 'a'].Add(i);
convertMap[i] = str2[i];
}
}
// Calculate result
// Initializing return values
int ret = 0;
List<List<int>> retv = new List<List<int>>();
// Iterating the character from
// the end
for(int i = 25; i >= 0; i--)
{
List<int> v = convChar[i];
if (v.Count == 0)
continue;
// Increment the number of
// operations
ret++;
List<int> v1 = str1array[i];
// Not possible to convert
if (v1.Count == 0)
{
Console.WriteLine(-1);
return;
}
// To check whether the final
// element has been added
// in set S or not.
bool isScompleted = false;
for(int j = 0; j < v1.Count; j++)
{
// Check if v1[j] is present
// in hashmap or not
if (convertMap.ContainsKey(v1[j]))
{
char a = convertMap[v1[j]];
// Already converted then
// then continue
if (a > i + 'a')
continue;
else
{
v.Add(v1[j]);
isScompleted = true;
retv.Add(v);
break;
}
}
else
{
v.Add(v1[j]);
isScompleted = true;
retv.Add(v);
break;
}
}
// Not possible to convert
if (!isScompleted)
{
Console.WriteLine(-1);
return;
}
}
// Print the result
Console.WriteLine(ret);
}
// Driver code
static void Main()
{
// Given strings
string A = "abcab";
string B = "aabab";
// Function call
transformString(A, B);
}
}
// This code is contributed by divyesh072019
JavaScript
<script>
// JavaScript program for the above approach
// Function to return the minimum number
// of operation
function transformString(str1, str2)
{
// Storing data
let N = str1.length
let convChar = []
let str1array = []
// Initialize both arrays
for(let i = 0; i < 26; i++)
{
convChar.push([])
str1array.push([])
}
// Stores the index of character
let convertMap = new Map()
// Filling str1array, convChar
// and hashmap convertMap.
for(let i = 0; i < N; i++)
{
str1array[str1.charCodeAt(i) - 'a'.charCodeAt(0)].push(i)
}
for(let i = 0; i < N; i++)
{
// Not possible to convert
if (str1.charCodeAt(i) < str2.charCodeAt(i))
{
document.write(-1)
return
}
else if (str1[i] == str2[i])
continue
else
convChar[str2.charCodeAt(i) - 'a'.charCodeAt(0)].push(i)
convertMap[i] = str2[i]
}
// Calculate result
// Initializing return values
let ret = 0
let retv = []
// Iterating the character from
// the end
for(let i = 25; i >= 0; i--)
{
let v = convChar[i]
if (v.length == 0)
continue
// Increment the number of
// operations
ret += 1;
v1 = str1array[i]
// Not possible to convert
if (v1.length == 0){
document.write(-1)
return
}
// To check whether the final
// element has been added
// in set S or not.
isScompleted = false
for(let j = 0; j < v1.length; j++)
{
// Check if v1[j] is present
// in hashmap or not
if (convertMap.has(v1[j])){
let a = v1[j]
// Already converted then
// then continue
if (a > i + 'a'.charCodeAt(0))
continue
else{
v.push(v1[j])
isScompleted = true
retv.append(v)
break
}
}
else{
v.push(v1[j])
isScompleted = true
retv.push(v)
break
}
}
// Not possible to convert
if (isScompleted == false){
document.write(-1)
return
}
}
// Print the result
document.write(ret)
}
// Driver Code
let A = "abcab"
let B = "aabab"
// Function call
transformString(A, B)
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read