📌  相关文章
📜  通过任意次数替换字符串之间的字符,将给定的字符串转换为 T

📅  最后修改于: 2021-10-28 01:36:54             🧑  作者: Mango

给定一个由N 个字符串的数组arr[]和一个大小为M的字符串T ,任务是检查是否可以通过从一个字符串删除任何字符来使数组arr[]中的所有字符串与字符串T相同,说arr[i]并将其插入到另一个字符串arr[j] 的任何位置 任意次数。

例子:

方法:可以基于以下观察来解决给定的问题,即当且仅当满足以下条件时,输出将为“”:

  • None字符串包含T 中不存在的任何字符。
  • T 的所有字符必须在S[]组合的所有给定字符串中出现N次。

请按照以下步骤解决问题:

  • 初始化两个数组,例如freqS[256]freqT[256] ,其值为0,以分别存储数组中所有字符串arr[]和字符串T 中出现的字符的频率。
  • 遍历给定的字符串数组arr[]并将每个字符串的字符频率存储在数组freqS[] 中
  • 迭代字符串T的字符和字符串的T字符的频率存储阵列freqT []英寸
  • 使用变量i在范围[0, 255] 中迭代并执行以下步骤:
    • 如果freqS[i]freqT[i]的值为0freqS[i] 的值等于N*freq[T] ,则继续迭代。
    • 否则,初始化一个布尔变量,说A并跳出循环。
  • 完成上述步骤后,如果A的值为真,则打印“No” 。否则,打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it possible
// to make all the strings equal to
// the string T
string checkIfPossible(int N, string arr[], string T)
{
   
    // Stores the frequency of all
    // the strings in the array arr[]
    int freqS[256] = {0};
 
    // Stores the frequency of the
    // string T
    int freqT[256] = {0};
 
    // Iterate over the characters
    // of the string T
    for (char ch : T) {
        freqT[ch - 'a']++;
    }
 
    // Iterate in the range [0, N-1]
    for (int i = 0; i < N; i++) {
 
        // Iterate over the characters
        // of the string arr[i]
        for (char ch : arr[i]) {
            freqS[ch - 'a']++;
        }
    }
 
    for (int i = 0; i < 256; i++) {
 
        // If freqT[i] is 0 and
        // freqS[i] is not 0
        if (freqT[i] == 0
            && freqS[i] != 0) {
            return "No";
        }
 
        // If freqS[i] is 0 and
        // freqT[i] is not 0
        else if (freqS[i] == 0
                 && freqT[i] != 0) {
            return "No";
        }
 
        // If freqS[i] is not freqT[i]*N
        else if (freqT[i] != 0
                 && freqS[i]
                        != (freqT[i] * N)) {
            return "No";
        }
    }
 
    // Otherwise, return "Yes"
    return "Yes";
}
 
// Driver Code
int main() {
 
    string arr[] = { "abc", "abb", "acc" };
    string T = "abc";
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << checkIfPossible(N, arr, T);
    return 0;
}
 
// This code is contributed by Dharanendra L V.


Java
// Java program for the above approach
 
public class GFG {
 
    // Function to check if it possible
    // to make all the strings equal to
    // the string T
    static String checkIfPossible(
        int N, String[] arr, String T)
    {
        // Stores the frequency of all
        // the strings in the array arr[]
        int[] freqS = new int[256];
 
        // Stores the frequency of the
        // string T
        int[] freqT = new int[256];
 
        // Iterate over the characters
        // of the string T
        for (char ch : T.toCharArray()) {
            freqT[ch - 'a']++;
        }
 
        // Iterate in the range [0, N-1]
        for (int i = 0; i < N; i++) {
 
            // Iterate over the characters
            // of the string arr[i]
            for (char ch : arr[i].toCharArray()) {
                freqS[ch - 'a']++;
            }
        }
 
        for (int i = 0; i < 256; i++) {
 
            // If freqT[i] is 0 and
            // freqS[i] is not 0
            if (freqT[i] == 0
                && freqS[i] != 0) {
                return "No";
            }
 
            // If freqS[i] is 0 and
            // freqT[i] is not 0
            else if (freqS[i] == 0
                     && freqT[i] != 0) {
                return "No";
            }
 
            // If freqS[i] is not freqT[i]*N
            else if (freqT[i] != 0
                     && freqS[i]
                            != (freqT[i] * N)) {
                return "No";
            }
        }
 
        // Otherwise, return "Yes"
        return "Yes";
    }
 
    // Driver Code
    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
# Python3 program for the above approach
 
# Function to check if it possible
# to make all the strings equal to
# the T
def checkIfPossible(N, arr, T):
 
    # Stores the frequency of all
    # the strings in the array arr[]
    freqS = [0] * 256
 
    # Stores the frequency of the
    # T
    freqT = [0] * 256
 
    # Iterate over the characters
    # of the T
    for ch in T:
        freqT[ord(ch) - ord('a')] += 1
 
    # Iterate in the range [0, N-1]
    for i in range(N):
         
        # Iterate over the characters
        # of the arr[i]
        for ch in arr[i]:
            freqS[ord(ch) - ord('a')] += 1
 
    for i in range(256):
         
        # If freqT[i] is 0 and
        # freqS[i] is not 0
        if (freqT[i] == 0 and freqS[i] != 0):
            return "No"
 
        # If freqS[i] is 0 and
        # freqT[i] is not 0
        elif (freqS[i] == 0 and freqT[i] != 0):
            return "No"
             
        # If freqS[i] is not freqT[i]*N
        elif (freqT[i] != 0 and freqS[i]!= (freqT[i] * N)):
            return "No"
 
    # Otherwise, return "Yes"
    return "Yes"
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ "abc", "abb", "acc" ]
    T = "abc"
    N = len(arr)
     
    print(checkIfPossible(N, arr, T))
 
# This code is contributed by mohit kumar 29


C#
// c# program for the above approach
using System;
public class GFG {
 
    // Function to check if it possible
    // to make all the strings equal to
    // the string T
    static string checkIfPossible(int N, string[] arr,
                                  string T)
    {
        // Stores the frequency of all
        // the strings in the array arr[]
        int[] freqS = new int[256];
 
        // Stores the frequency of the
        // string T
        int[] freqT = new int[256];
 
        // Iterate over the characters
        // of the string T
        foreach(char ch in T.ToCharArray())
        {
            freqT[ch - 'a']++;
        }
 
        // Iterate in the range [0, N-1]
        for (int i = 0; i < N; i++) {
 
            // Iterate over the characters
            // of the string arr[i]
            foreach(char ch in arr[i].ToCharArray())
            {
                freqS[ch - 'a']++;
            }
        }
 
        for (int i = 0; i < 256; i++) {
 
            // If freqT[i] is 0 and
            // freqS[i] is not 0
            if (freqT[i] == 0 && freqS[i] != 0) {
                return "No";
            }
 
            // If freqS[i] is 0 and
            // freqT[i] is not 0
            else if (freqS[i] == 0 && freqT[i] != 0) {
                return "No";
            }
 
            // If freqS[i] is not freqT[i]*N
            else if (freqT[i] != 0
                     && freqS[i] != (freqT[i] * N)) {
                return "No";
            }
        }
 
        // Otherwise, return "Yes"
        return "Yes";
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string[] arr = { "abc", "abb", "acc" };
        string T = "abc";
        int N = arr.Length;
        Console.WriteLine(checkIfPossible(N, arr, T));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
Yes

时间复杂度: O(N*L + M),其中L是数组 arr[] 中最长字符串的长度。
辅助空间: O(26)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程