


Converts the given string to T, by replacing characters between strings any number of times
Converting a string means we have to make it same as the given string based on the given condition. In this question, we are given an array consisting of string "arr" and string "T" of size "M". Our task is to check if all the strings present in the array can be made identical to the given one by removing any character from the string ( arr[i] ) of the array and inserting that character into any index of another string String T A string of the same array ( arr[j] ). We can do this as many times as we like. Returns "YES" if all strings in the array can be made identical to string 'T', otherwise returns "NO".
Example
Input 1: arr = [ “wxyz”, “wxxy”, “wyzz” ], T = “wxyz”
Output 1: YES
illustrate
One of the possible ways to make all strings in the array identical to the string T is as follows -
Delete the characters of the string arr[1] ("wxxy") at index 2 and insert them into the string arr[2] ("wyzz") at index 1. Then it looks like: ["wxyz","wxy","wxyzz"]
Delete the characters of the string arr[2] ("wxyzz") at index 3 and insert them into the string arr[1] ("wxy") at index 3. Then it looks like: ["wxyz","wxyz","wxyz"].
After performing the above steps, we can make all strings in the array the same as the string T. So the answer is "YES".
Input 2: arr = [ “rts”, “rtw”, “rts” ], T = “rts”
Output 2: NO
illustrate
There are 3 strings in the array, 2 of which are the same as string T, but the string with index number 1 is different. It contains different characters that are not part of the string T. It is not possible to make all strings in the array a string T. Therefore, the answer is "NO".
Method: Use Hashmap
We have seen the example of the given string above, let us move to the method -
We have two observations as follows -
Because we must make all strings in the array the same as string T, so that all characters of each string in the array must appear in string T. In other words, there are no different characters. Otherwise, we cannot meet the conditions.
After we calculate the frequency of occurrence of characters for all strings in the array, the frequency of occurrence of each character must be equal to the size of array "N".
< /里>
Based on the above observations, we have two conditions to check.
The hash map of strings of size array "freqArr" is equal to the hash map "freqT" of string "T". As
freqArr.size() == freqT.size()
Every character of string T should appear in every string in the array. Each character of string T should have a frequency count of "N" in the array string. as-
freqArr.find(T[i]) == freqArr.end() and freqArr[T[i]] != freqT[T[i]]*N.
We can use hashing to solve this problem because we need to calculate the frequency of characters in the array string and string T.
Example
Let us see the code of the above method for better understanding -
// Program to convert all strings to T #include <bits/stdc++.h> using namespace std; string covertStringIntoT( int N, string arr[], string T){ map< char,int > freqT; //to store the frequency of each character of string T int len = T.size(); //getting the size of the string T //traverse the string T to store the frequency of the characters for( int i=0; i<len; i++){ freqT[T[i]]++; } map< char,int > freqArr; //to store the frequency of each chracter of strings // of Array. //traverse the strings of Array to store the frequency of the characters for( int i=0; i<N; i++){ for(int j=0;j<arr[i].size(); j++){ freqArr[arr[i][j]]++; } } // Check the condition one if(freqT.size() != freqArr.size()){ return "NO"; } //check condition two while trversing the string T for( int i=0; i<len; i++){ if(freqArr.find(T[i]) == freqArr.end() || freqArr[T[i]] != freqT[T[i]]*N ){ return "NO"; } } return "YES"; } int main() { string T = "wxyz"; // given string string arr[] = {"wxyz", "wxyy", "wxzz"}; // given array of strings int N = sizeof(arr) / sizeof(arr[0]); //getting the size of the array of string // calling the function 'convertStringIntoT' to convert all strings of the // array into string T string result = covertStringIntoT( N, arr, T); if(result == "YES"){ cout<< result << ", it is possible to make all the strings of the array as string T"; } else{ cout<< result << ", it is not possible to make all the strings of the array as string T"; } return 0; }
Output
YES, it is possible to make all the strings of the array as string T
Time and space complexity
The time complexity of the above code is O(M N*L)
The space complexity of the above code is O(M)
Where M is the size of the string T, N is the size of the array, and L is the longest string present in the array.
in conclusion
In this tutorial, we implemented a program that converts a given string into a T by replacing characters between the strings as many times as necessary. We implemented a hashing method because we had to store the frequencies. In this method, we mainly check two conditions, if all conditions are met, it means that we are able to convert all the strings in the array into the same string as the string T.
The above is the detailed content of Converts the given string to T, by replacing characters between strings any number of times. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values in PHP and give specific code examples. 1. Change the characters

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.
