Convert given Strings into T by replacing characters in between strings any number of times
Given an array, arr[] of N strings and a string T of size M, the task is to check if it is possible to make all the strings in the array arr[] same as the string T by removing any character from one string, say arr[i] and inserting it at any position to another string arr[j] any number of times.
Examples:
Input: arr[] = {“abc”, “abb”, “acc”}, T = “abc”
Output: Yes
Explanation:
Below is one of the possible way to make the all strings in the array to the string T is:
- Remove character at index 2 of the string arr[1](= “abb”) and then insert it at the index 1 of the string arr[2](= “acc”). After this the array modifies to {“abc”, “”ab”, “abcc”}.
- Remove character at index 3 of the string arr[2](= “abcc”) and then insert it at the index 2 of the string arr[1]( = “ab”). After this the array modifies to {“abc”, “”abc”, “abc”}.
After the above steps, all the strings of the array arr[] are equal to the string T(= abc). Therefore, print Yes.
Input: arr[] = {“abc”, “bbb”, “ccc”}, T = “abc”
Output: No
Approach: The given problem can be solved based on the following observation that the output will be “Yes” if and only if the following conditions are satisfied:
- None the string contains any character which is not present in T.
- All the characters of T must be present N times in all the given strings of S[] combined.
Follow the steps to solve the problem:
- Initialize two arrays, say freqS[256] and freqT[256] with value 0 to store the frequency of characters present in all strings of the array, arr[] and the string T respectively.
- Traverse the given array of strings arr[] and store the frequency of character for every string in the array freqS[].
- Iterate over the characters of the string T and store the frequency of character of the string T in the array freqT[].
- Iterate in the range [0, 255] using the variable i and perform the following steps:
- After completing the above steps, if the value of A is true, then print “No”. Otherwise, print “Yes“.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string checkIfPossible( int N, string arr[], string T)
{
int freqS[256] = {0};
int freqT[256] = {0};
for ( char ch : T) {
freqT[ch - 'a' ]++;
}
for ( int i = 0; i < N; i++) {
for ( char ch : arr[i]) {
freqS[ch - 'a' ]++;
}
}
for ( int i = 0; i < 256; i++) {
if (freqT[i] == 0
&& freqS[i] != 0) {
return "No" ;
}
else if (freqS[i] == 0
&& freqT[i] != 0) {
return "No" ;
}
else if (freqT[i] != 0
&& freqS[i]
!= (freqT[i] * N)) {
return "No" ;
}
}
return "Yes" ;
}
int main() {
string arr[] = { "abc" , "abb" , "acc" };
string T = "abc" ;
int N = sizeof (arr) / sizeof (arr[0]);
cout << checkIfPossible(N, arr, T);
return 0;
}
|
Java
public class GFG {
static String checkIfPossible(
int N, String[] arr, String T)
{
int [] freqS = new int [ 256 ];
int [] freqT = new int [ 256 ];
for ( char ch : T.toCharArray()) {
freqT[ch - 'a' ]++;
}
for ( int i = 0 ; i < N; i++) {
for ( char ch : arr[i].toCharArray()) {
freqS[ch - 'a' ]++;
}
}
for ( int i = 0 ; i < 256 ; i++) {
if (freqT[i] == 0
&& freqS[i] != 0 ) {
return "No" ;
}
else if (freqS[i] == 0
&& freqT[i] != 0 ) {
return "No" ;
}
else if (freqT[i] != 0
&& freqS[i]
!= (freqT[i] * N)) {
return "No" ;
}
}
return "Yes" ;
}
public static void main(String[] args)
{
String[] arr = { "abc" , "abb" , "acc" };
String T = "abc" ;
int N = arr.length;
System.out.println(
checkIfPossible(N, arr, T));
}
}
|
Python3
def checkIfPossible(N, arr, T):
freqS = [ 0 ] * 256
freqT = [ 0 ] * 256
for ch in T:
freqT[ ord (ch) - ord ( 'a' )] + = 1
for i in range (N):
for ch in arr[i]:
freqS[ ord (ch) - ord ( 'a' )] + = 1
for i in range ( 256 ):
if (freqT[i] = = 0 and freqS[i] ! = 0 ):
return "No"
elif (freqS[i] = = 0 and freqT[i] ! = 0 ):
return "No"
elif (freqT[i] ! = 0 and freqS[i]! = (freqT[i] * N)):
return "No"
return "Yes"
if __name__ = = '__main__' :
arr = [ "abc" , "abb" , "acc" ]
T = "abc"
N = len (arr)
print (checkIfPossible(N, arr, T))
|
C#
using System;
public class GFG {
static string checkIfPossible( int N, string [] arr,
string T)
{
int [] freqS = new int [256];
int [] freqT = new int [256];
foreach ( char ch in T.ToCharArray())
{
freqT[ch - 'a' ]++;
}
for ( int i = 0; i < N; i++) {
foreach ( char ch in arr[i].ToCharArray())
{
freqS[ch - 'a' ]++;
}
}
for ( int i = 0; i < 256; i++) {
if (freqT[i] == 0 && freqS[i] != 0) {
return "No" ;
}
else if (freqS[i] == 0 && freqT[i] != 0) {
return "No" ;
}
else if (freqT[i] != 0
&& freqS[i] != (freqT[i] * N)) {
return "No" ;
}
}
return "Yes" ;
}
public static void Main( string [] args)
{
string [] arr = { "abc" , "abb" , "acc" };
string T = "abc" ;
int N = arr.Length;
Console.WriteLine(checkIfPossible(N, arr, T));
}
}
|
Javascript
<script>
function checkIfPossible(N, arr, T)
{
let freqS = new Array(256).fill(0);
let freqT = new Array(256).fill(0);
for (let ch of T) {
freqT[ch.charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
for (let i = 0; i < N; i++) {
for (let ch of arr[i]) {
freqS[ch.charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
}
for (let i = 0; i < 256; i++) {
if (freqT[i] == 0 && freqS[i] != 0) {
return "No" ;
}
else if (freqS[i] == 0 && freqT[i] != 0) {
return "No" ;
}
else if (freqT[i] != 0
&& freqS[i]
!= (freqT[i] * N)) {
return "No" ;
}
}
return "Yes" ;
}
let arr = [ "abc" , "abb" , "acc" ];
let T = "abc" ;
let N = arr.length;
document.write(checkIfPossible(N, arr, T));
</script>
|
Time Complexity: O(N*L + M), Where L is the length of the longest string in the array arr[].
Auxiliary Space: O(26)
Last Updated :
21 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...