📌  相关文章
📜  检查字符串包含两个K长度不重叠的子字符串作为字谜

📅  最后修改于: 2021-05-04 18:09:13             🧑  作者: Mango

给定长度为N的字符串str和整数K ,任务是检查字符串是否有两个长度为K的不重叠子字符串作为字谜。

例子:

方法:解决此问题的想法是遍历给定的字符串,并使用一组存储长度为K的子字符串,并搜索给定字符串存在的两个不重叠的子字符串。请按照以下步骤操作:

  • 初始化一个unordered_set以存储长度为K的子字符串。
  • 使用变量i遍历给定字符串str的字符。
  • 如果存在长度为K的字符串起始索引(i – 1)的STR,然后擦除设置长度K的排序字符串。
  • 如果存在在索引(i – 1)结束长度K的字符串str然后插入长度K的排序字符串插入
  • 如果在以下位置找到了从索引i开始长度为K排序后的子字符串: 集合中,则存在两个长度为K的不重叠子串,如str中的字谜。因此,打印“是”并退出循环。否则,将从索引i开始的长度为K的排序后的子字符串插入Set中
  • 如果在遍历整个字符串后未找到一对子字符串,请打印“ No”。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
using namespace std;
 
// Function to check whether the string
// s has two non-overlapping substrings
// of length K as anagrams
void anagramPairs(string str, int K)
{
    // Stores the substrings of length K
    unordered_set set;
    int l = str.length();
 
    // Iterate through every character
    for (int i = 0; i < l; i++) {
 
        // If there is a substring starting
        // at index i - 1 of length K then
        // erase that substring from set
        if (i > 0 && K - (i - 1) - 1 < l) {
            string s1 = str.substr(i - 1, K);
 
            // Sort the substring
            sort(s1.begin(), s1.end());
 
            // Remove from set
            set.erase(s1);
        }
 
        // If there is a substring of length
        // K ending at index i - 1
        if ((i - 1) - K + 1 >= 0) {
 
            string s1 = str.substr(
                (i - 1) - K + 1, K);
 
            // Sort the substring
            sort(s1.begin(), s1.end());
 
            // Insert substring into the Set
            set.insert(s1);
        }
 
        // If there is a substring of length
        // K starting from the i-th index
        if (K + i - 1 < l) {
 
            // Check if the sorted
            // substring is present in
            // the set or not
            string s1 = str.substr(i, K);
 
            sort(s1.begin(), s1.end());
 
            // If present in the Set
            if (set.count(s1)) {
                cout << "Yes";
                return;
            }
 
            // Insert the sorted
            // substring into the set
            set.insert(s1);
        }
    }
 
    // If not present in the Set
    cout << "No";
}
 
// Driver Code
int main()
{
    string str = "ginfing";
    int K = 3;
 
    // Function Call
    anagramPairs(str, K);
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG
{
 
// Function to check whether the String
// s has two non-overlapping subStrings
// of length K as anagrams
static void anagramPairs(String str, int K)
{
   
    // Stores the subStrings of length K
    HashSet set = new HashSet();
    int l = str.length();
 
    // Iterate through every character
    for (int i = 0; i < l; i++)
    {
 
        // If there is a subString starting
        // at index i - 1 of length K then
        // erase that subString from set
        if (i > 0 && K - (i - 1) - 1 < l)
        {
            String s1 = str.substring(i - 1, K);
 
            // Sort the subString
            s1 = sortString(s1);
 
            // Remove from set
            set.remove(s1);
        }
 
        // If there is a subString of length
        // K ending at index i - 1
        if ((i - 1) - K + 1 >= 0)
        {
 
            String s1 = str.substring(
                (i - 1) - K + 1, K);
 
            // Sort the subString
            s1 = sortString(s1);
 
            // Insert subString into the Set
            set.add(s1);
        }
 
        // If there is a subString of length
        // K starting from the i-th index
        if (K + i - 1 < l)
        {
 
            // Check if the sorted
            // subString is present in
            // the set or not
            String s1 = str.substring(i, i+K);
 
            s1 = sortString(s1);
 
            // If present in the Set
            if (set.contains(s1))
            {
                System.out.print("Yes");
                return;
            }
 
            // Insert the sorted
            // subString into the set
            set.add(s1);
        }
    }
 
    // If not present in the Set
    System.out.print("No");
}
static String sortString(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 = "ginfing";
    int K = 3;
 
    // Function Call
    anagramPairs(str, K);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to check whether the string
# s has two non-overlapping substrings
# of length K as anagrams
def anagramPairs(str, K):
     
    # Stores the substrings of length K
    sett = {}
    l = len(str)
 
    # Iterate through every character
    for i in range(l):
 
        # If there is a substring starting
        # at index i - 1 of length K then
        # erase that substring from sett
        if (i > 0 and K - (i - 1) - 1 < l):
            s1 = str[i - 1:i + K - 1]
 
            # Sort the substring
            s1 = sorted(s1)
 
            # Remove from sett
            del sett["".join(s1)]
 
        # If there is a substring of length
        # K ending at index i - 1
        if ((i - 1) - K + 1 >= 0):
 
            s1 = str[(i - 1) - K + 1:i]
 
            # Sort the substring
            s1 = sorted(s1)
 
            # Insert substring into the Set
            sett["".join(s1)] = 1
 
        # If there is a substring of length
        # K starting from the i-th index
        if (K + i - 1 < l):
 
            # Check if the sorted
            # substring is present in
            # the sett or not
            s1 = str[i : i + K]
 
            s1 = sorted(s1)
 
            # If present in the Set
            if "".join(s1) in sett:
                print("Yes")
                return
 
            #Insert the sorted
            # substring into the sett
            sett["".join(s1)] = 1
 
    # If not present in the Set
    print("No")
 
# Driver Code
if __name__ == '__main__':
    str = "ginfing"
    K = 3
 
    # Function Call
    anagramPairs(str, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to check whether the String
// s has two non-overlapping subStrings
// of length K as anagrams
static void anagramPairs(String str, int K)
{
   
    // Stores the subStrings of length K
    HashSet set = new HashSet();
    int l = str.Length;
 
    // Iterate through every character
    for (int i = 0; i < l; i++)
    {
 
        // If there is a subString starting
        // at index i - 1 of length K then
        // erase that subString from set
        if (i > 0 && K - (i - 1) - 1 < l)
        {
            String s1 = str.Substring(i - 1, K);
 
            // Sort the subString
            s1 = sortString(s1);
 
            // Remove from set
            set.Remove(s1);
        }
 
        // If there is a subString of length
        // K ending at index i - 1
        if ((i - 1) - K + 1 >= 0)
        {
 
            String s1 = str.Substring(
                (i - 1) - K + 1, K);
 
            // Sort the subString
            s1 = sortString(s1);
 
            // Insert subString into the Set
            set.Add(s1);
        }
 
        // If there is a subString of length
        // K starting from the i-th index
        if (K + i - 1 < l)
        {
 
            // Check if the sorted
            // subString is present in
            // the set or not
            String s1 = str.Substring(i, K);
 
            s1 = sortString(s1);
 
            // If present in the Set
            if (set.Contains(s1))
            {
                Console.Write("Yes");
                return;
            }
 
            // Insert the sorted
            // subString into the set
            set.Add(s1);
        }
    }
 
    // If not present in the Set
    Console.Write("No");
}
static String sortString(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 = "ginfing";
    int K = 3;
 
    // Function Call
    anagramPairs(str, K);
}
}
 
// This code is contributed by Princi Singh


输出:
Yes

时间复杂度: O(N *(K + K * log K))
辅助空间: O(N)