📌  相关文章
📜  检查一个字符串包含另一个字符串的字谜作为其子字符串

📅  最后修改于: 2021-04-18 02:23:21             🧑  作者: Mango

给定两个字符串S1S2 ,任务是检查S2是否包含S1字谜作为其子字符串。

例子:

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

  1. 初始化两个Hashmap s1hashs2hash ,以存储两个字符串的字母频率。
  2. 如果S1的长度大于S2的长度,则打印“否”
  3. 遍历字符串S1的字符并更新s1hash
  4. 使用滑动窗口技术遍历字符串S2的字符,并相应地更新HashMap。
  5. 对于长度等于S1的S2的任何子字符串,如果发现两个哈希图相等,则打印“ YES”
  6. 否则,打印“否”

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if string s2
// contains anagram of the string
// s1 as its substring
bool checkAnagram(string s1, string s2)
{
    // Stores frequencies of
    // characters in substrings of s2
    vector s2hash(26, 0);
 
    // Stores frequencies of
    // characters in s1
    vector s1hash(26, 0);
    int s1len = s1.size();
    int s2len = s2.size();
 
    // If length of s2 exceeds length of s1
    if (s1len > s2len)
        return false;
 
    int left = 0, right = 0;
 
    // Store frequencies of characters in first
    // substring of length s1len in string s2
    while (right < s1len) {
 
        s1hash[s1[right] - 'a'] += 1;
        s2hash[s2[right] - 'a'] += 1;
        right++;
    }
 
    right -= 1;
 
    // Perform Sliding Window technique
    while (right < s2len) {
 
        // If hashmaps are found to be
        // identical for any substring
        if (s1hash == s2hash)
            return true;
 
        right++;
 
        if (right != s2len)
            s2hash[s2[right] - 'a'] += 1;
 
        s2hash[s2[left] - 'a'] -= 1;
        left++;
    }
    return false;
}
 
// Driver Code
int main()
{
    string s1 = "ab";
    string s2 = "bbpobac";
 
    if (checkAnagram(s1, s2))
        cout << "YES\n";
    else
        cout << "No\n";
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to check if string s2
  // contains anagram of the string
  // s1 as its substring
  public static boolean checkAnagram(String s1, String s2)
  {
 
    // Stores frequencies of
    // characters in substrings of s2
    int s2hash[] = new int[26];
 
    // Stores frequencies of
    // characters in s1
    int s1hash[] = new int[26];
    int s1len = s1.length();
    int s2len = s2.length();
 
    // If length of s2 exceeds length of s1
    if (s1len > s2len)
      return false;
    int left = 0, right = 0;
 
    // Store frequencies of characters in first
    // substring of length s1len in string s2
    while (right < s1len)
    {
      s1hash[s1.charAt(right) - 'a'] += 1;
      s2hash[s2.charAt(right) - 'a'] += 1;
      right++;
    }
 
    right -= 1;
 
    // Perform Sliding Window technique
    while (right < s2len) {
 
      // If hashmaps are found to be
      // identical for any substring
      if (Arrays.equals(s1hash, s2hash))
        return true;
 
      right++;
 
      if (right != s2len)
        s2hash[s2.charAt(right) - 'a'] += 1;
 
      s2hash[s2.charAt(left) - 'a'] -= 1;
      left++;
    }
    return false;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    String s1 = "ab";
    String s2 = "bbpobac";
 
    if (checkAnagram(s1, s2))
      System.out.println("YES");
    else
      System.out.println("No");
  }
}
 
// This code is contributed by kingash.


Python3
# Python 3 Program to implement
# the above approach
 
# Function to check if string s2
# contains anagram of the string
# s1 as its substring
def checkAnagram(s1, s2):
    # Stores frequencies of
    # characters in substrings of s2
    s2hash = [0 for i in range(26)]
 
    # Stores frequencies of
    # characters in s1
    s1hash = [0 for i in range(26)]
    s1len = len(s1)
    s2len = len(s2)
 
    # If length of s2 exceeds length of s1
    if (s1len > s2len):
        return False
 
    left = 0
    right = 0
 
    # Store frequencies of characters in first
    # substring of length s1len in string s2
    while (right < s1len):
        s1hash[ord(s1[right]) - 97] += 1
        s2hash[ord(s2[right]) - 97] += 1
        right += 1
 
    right -= 1
 
    # Perform Sliding Window technique
    while (right < s2len):
        # If hashmaps are found to be
        # identical for any substring
        if (s1hash == s2hash):
            return True
        right += 1
 
        if (right != s2len):
            s2hash[ord(s2[right]) - 97] += 1
 
        s2hash[ord(s2[left]) - 97] -= 1
        left += 1
 
    return False
 
# Driver Code
if __name__ == '__main__':
    s1 = "ab"
    s2 = "bbpobac"
 
    if (checkAnagram(s1, s2)):
        print("YES")
    else:
        print("No")
         
        # This code is contributed by ipg2016107


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
    
// Function to check if string s2
// contains anagram of the string
// s1 as its substring
static bool checkAnagram(string s1, string s2)
{
    // Stores frequencies of
    // characters in substrings of s2
    List s2hash = new List();
    for(int i=0;i<26;i++)
        s2hash.Add(0);
 
    // Stores frequencies of
    // characters in s1
    List s1hash = new List();
    for(int i=0;i<26;i++)
        s1hash.Add(0);
    int s1len = s1.Length;
    int s2len = s2.Length;
 
    // If length of s2 exceeds length of s1
    if (s1len > s2len)
        return false;
 
    int left = 0, right = 0;
 
    // Store frequencies of characters in first
    // substring of length s1len in string s2
    while (right < s1len) {
 
        s1hash[s1[right] - 'a'] += 1;
        s2hash[s2[right] - 'a'] += 1;
        right++;
    }
 
    right -= 1;
 
    // Perform Sliding Window technique
    while (right < s2len) {
 
        // If hashmaps are found to be
        // identical for any substring
        if (s1hash == s2hash)
            return true;
 
        right++;
 
        if(right != s2len)
            s2hash[s2[right] - 'a'] += 1;
 
        s2hash[s2[left] - 'a'] -= 1;
        left++;
    }
    return false;
}
 
// Driver Code
public static void Main()
{
    string s1 = "ab";
    string s2 = "bbpobac";
 
    if (checkAnagram(s1, s2)==true)
        Console.WriteLine("NO");
    else
        Console.WriteLine("YES");
}
}
 
// This code is contributed by bgangawar59.


输出:
YES

时间复杂度: O(26 * len(S2))
辅助空间: O(26)