📌  相关文章
📜  通过附加字符可以使数组 A 中的字符串计数等于数组 B 中的字符串

📅  最后修改于: 2022-05-13 01:56:06.895000             🧑  作者: Mango

通过附加字符可以使数组 A 中的字符串计数等于数组 B 中的字符串

给定两个字符串数组 SearchWord[ ]FindWord[] 。任务是检查在对尽可能多的字符串进行以下操作后,可以形成多少个 FindWord[]中的字符串:

  • SearchWord[]中选择一个字符串。
  • 添加一个英文字母(该字符串中不存在)。
  • 根据需要重新排列字符串。

注意: SearchWord[ ]FindWord[]中的每个字母都是唯一的,并且只出现一次。

例子:

方法:解决这个问题的简单方法是排序和使用散列技术。

  • 按字母顺序对 SearchWord[ ]FindWord[]进行排序。
  • 应用相反的逻辑,而不是从 SearchWord[] 数组中挑选一个词,而是从 FindWord[] 数组中挑选一个词。
  • 并通过逐个删除字符并找到匹配词来搜索该词。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to find permutation
int solution(vector& sw,
             vector& tw)
{
    unordered_set s;
    for (int i = 0; i < sw.size(); i++) {
        sort(sw[i].begin(), sw[i].end());
        s.insert(sw[i]);
    }
    int ans = 0;
 
    // Loop to check how many string
    // can be formed
    for (int i = 0; i < tw.size(); i++) {
        string test = tw[i];
        sort(test.begin(), test.end());
 
        // To track the number
        // Of the words formed
        bool check = 0;
 
        for (int j = 0; j < test.size(); j++) {
 
            // Substring from 0 to 'i'
            // and remove one alphabet
            // and append rest of the string
            string search
                = test.substr(0, j)
                  + test.substr(j + 1);
 
            // Check if the word is available
            // in the set
            if (s.find(search) != s.end()) {
                check = 1;
                break;
            }
        }
        if (check)
            ans++;
    }
    return ans;
}
 
// Driver Code
int main()
{
    vector str = { "ohr", "tm", "ek" };
    vector str1 = { "mat", "hr" };
 
    cout << solution(str, str1);
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
class GFG{
 
  // Function to find permutation
  static int solution(String[] sw,
                      String[]  tw)
  {
    HashSet s = new HashSet<>();
    for (int i = 0; i < sw.length; i++) {
      sw[i] = sort(sw[i]);
      s.add(sw[i]);
    }
    int ans = 0;
 
    // Loop to check how many String
    // can be formed
    for (int i = 0; i < tw.length; i++) {
      String test = tw[i];
      test = sort(test);
 
      // To track the number
      // Of the words formed
      boolean check = false;
 
      for (int j = 0; j < test.length(); j++) {
 
        // SubString from 0 to 'i'
        // and remove one alphabet
        // and append rest of the String
        String search
          = test.substring(0, j)
          + test.substring(j + 1);
 
        // Check if the word is available
        // in the set
        if (s.contains(search)) {
          check = true;
          break;
        }
      }
      if (check)
        ans++;
    }
    return ans;
  }
  //Method to sort a string alphabetically
  static String sort(String inputString)
  {
    // convert input string to char array
    char tempArray[] = inputString.toCharArray();
 
    // sort tempArray
    Arrays.sort(tempArray);
 
    // return new sorted string
    return new String(tempArray);
  }
   
  // Driver Code
  public static void main(String[] args)
  {
    String[] str = { "ohr", "tm", "ek" };
    String[]  str1 = { "mat", "hr" };
 
    System.out.print(solution(str, str1));
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# python3 code to implement the above approach
 
# Function to find permutation
def solution(sw, tw):
 
    s = set()
    for i in range(0, len(sw)):
        s.add("".join(sorted(list(sw[i]))))
 
    ans = 0
 
    # Loop to check how many string
    # can be formed
    for i in range(0, len(tw)):
        test = "".join(sorted(list(tw[i])))
 
        # To track the number
        # Of the words formed
        check = 0
 
        for j in range(0, len(test)):
 
        # Substring from 0 to 'i'
        # and remove one alphabet
        # and append rest of the string
            search = test[:j] + test[j + 1:]
 
            # Check if the word is available
            # in the set
            if (search in s):
                check = 1
                break
 
        if (check):
            ans += 1
 
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    str = ["ohr", "tm", "ek"]
    str1 = ["mat", "hr"]
 
    print(solution(str, str1))
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find permutation
  static int solution(String[] sw,
                      String[]  tw)
  {
    HashSet s = new HashSet();
    for (int i = 0; i < sw.Length; i++) {
      sw[i] = sort(sw[i]);
      s.Add(sw[i]);
    }
    int ans = 0;
 
    // Loop to check how many String
    // can be formed
    for (int i = 0; i < tw.Length; i++) {
      String test = tw[i];
      test = sort(test);
 
      // To track the number
      // Of the words formed
      bool check = false;
 
      for (int j = 0; j < test.Length; j++) {
 
        // SubString from 0 to 'i'
        // and remove one alphabet
        // and append rest of the String
        String search
          = test.Substring(0, j)
          + test.Substring(j + 1);
 
        // Check if the word is available
        // in the set
        if (s.Contains(search)) {
          check = true;
          break;
        }
      }
      if (check)
        ans++;
    }
    return ans;
  }
  //Method to sort a string alphabetically
  static String sort(String inputString)
  {
     
    // convert input string to char array
    char []tempArray = inputString.ToCharArray();
 
    // sort tempArray
    Array.Sort(tempArray);
 
    // return new sorted string
    return new String(tempArray);
  }
   
  // Driver Code
  public static void Main(String[] args)
  {
    String[] str = { "ohr", "tm", "ek" };
    String[]  str1 = { "mat", "hr" };
 
    Console.Write(solution(str, str1));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
1

时间复杂度: O(N*M) 其中 N 和 M 是 SearchWord[] 和 FindWord[] 的大小
辅助空间: 在)