给定一个长度为N的字符串str和一个整数K ,任务是检查一个字符串是否有两个长度为K 的非重叠子串作为字谜。
例子:
Input: str = “ginfing”, K = 3
Output: Yes
Explanation:
“gin” and “ing” are the two non overlapping substrings of length 3 which are anagrams.
Therefore, the output is Yes.
Input: str = “ginig”, K = 3
Output: No
Explanation:
In the given string, there are no two non overlapping substrings of length 3 which are anagrams. Note that substring “gin” and substring “nig” are anagrams, but they are overlapping, hence are not considered.
Hence, the output is No.
方法:解决这个问题的思路是遍历给定的字符串,用一个集合来存储长度为K的子字符串,并搜索给定字符串存在的两个不重叠的子字符串。请按照以下步骤操作:
- 初始化 unordered_set集以存储长度为 K 的子字符串。
- 使用变量i迭代给定字符串str 的字符。
- 如果存在长度为K的字符串起始索引(i – 1)的STR,然后擦除从设置长度K的排序字符串。
- 如果存在在索引(i – 1)结束长度K的字符串str中,然后插入长度K的排序字符串插入集。
- 如果在索引i处找到长度为 K的排序子串 set ,那么在str 中存在两个长度为 K 的非重叠子串作为变位词。因此,打印“Yes”并跳出循环。否则,将从索引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
Javascript
Yes
时间复杂度: O(N*(K + K*log K))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。