给定字符串str1和str2 。任务是找出str1是否是str2的混洗形式的子字符串。如果str1是str2 的混洗形式的子字符串,则打印“YES”,否则打印“NO”。
例子
Input: str1 = “onetwofour”, str2 = “hellofourtwooneworld”
Output: YES
Explanation: str1 is substring in shuffled form of str2 as
str2 = “hello” + “fourtwoone” + “world”
str2 = “hello” + str1 + “world”, where str1 = “fourtwoone” (shuffled form)
Hence, str1 is a substring of str2 in shuffled form.
Input: str1 = “roseyellow”, str2 = “yellow”
Output: NO
Explanation: As the length of str1 is greater than str2. Hence, str1 is not a substring of str2.
方法:
令 n = str1 的长度,m = str2 的长度。
- 如果 n > m,则字符串str1永远不会是str2的子字符串。
- 否则对字符串str1 进行排序。
- 遍历字符串str2
- 将长度为 n 的str2的所有字符放入另一个字符串str 中。
- 对字符串str进行排序并比较str和str1 。
- 如果 str = str1,则字符串str1是字符串str2的混洗子字符串。
- 否则重复上述过程直到str2 的第 i 个索引使得 (i – n + 1 > m)(因为在此索引之后剩余字符串str2的长度将小于str1 。
- 如果上述步骤中 str 不等于 str1 ,则字符串str1永远不能是str2 的子字符串。
下面是上述方法的实现:
C++
// C++ program to check if string
// str1 is substring of str2 or not.
#include
using namespace std;
// Function two check string A
// is shuffled substring of B
// or not
bool isShuffledSubstring(string A, string B)
{
int n = A.length();
int m = B.length();
// Return false if length of
// string A is greater than
// length of string B
if (n > m) {
return false;
}
else {
// Sort string A
sort(A.begin(), A.end());
// Traverse string B
for (int i = 0; i < m; i++) {
// Return false if (i+n-1 >= m)
// doesn't satisfy
if (i + n - 1 >= m)
return false;
// Intialise the new string
string str = "";
// Copy the characters of
// string B in str till
// length n
for (int j = 0; j < n; j++)
str.push_back(B[i + j]);
// Sort the string str
sort(str.begin(), str.end());
// Return true if sorted
// string of "str" & sorted
// string of "A" are equal
if (str == A)
return true;
}
}
}
// Driver Code
int main()
{
// Input str1 and str2
string str1 = "geekforgeeks";
string str2 = "ekegorfkeegsgeek";
// Function return true if
// str1 is shuffled substring
// of str2
bool a = isShuffledSubstring(str1, str2);
// If str1 is substring of str2
// print "YES" else print "NO"
if (a)
cout << "YES";
else
cout << "NO";
cout << endl;
return 0;
}
Java
// Java program to check if String
// str1 is subString of str2 or not.
import java.util.*;
class GFG
{
// Function two check String A
// is shuffled subString of B
// or not
static boolean isShuffledSubString(String A, String B)
{
int n = A.length();
int m = B.length();
// Return false if length of
// String A is greater than
// length of String B
if (n > m)
{
return false;
}
else
{
// Sort String A
A = sort(A);
// Traverse String B
for (int i = 0; i < m; i++)
{
// Return false if (i + n - 1 >= m)
// doesn't satisfy
if (i + n - 1 >= m)
return false;
// Intialise the new String
String str = "";
// Copy the characters of
// String B in str till
// length n
for (int j = 0; j < n; j++)
str += B.charAt(i + j);
// Sort the String str
str = sort(str);
// Return true if sorted
// String of "str" & sorted
// String of "A" are equal
if (str.equals(A))
return true;
}
}
return false;
}
// 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 String.valueOf(tempArray);
}
// Driver Code
public static void main(String[] args)
{
// Input str1 and str2
String str1 = "geekforgeeks";
String str2 = "ekegorfkeegsgeek";
// Function return true if
// str1 is shuffled subString
// of str2
boolean a = isShuffledSubString(str1, str2);
// If str1 is subString of str2
// print "YES" else print "NO"
if (a)
System.out.print("YES");
else
System.out.print("NO");
System.out.println();
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to check if string
# str1 is subof str2 or not.
# Function two check A
# is shuffled subof B
# or not
def isShuffledSubstring(A, B):
n = len(A)
m = len(B)
# Return false if length of
# A is greater than
# length of B
if (n > m):
return False
else:
# Sort A
A = sorted(A)
# Traverse B
for i in range(m):
# Return false if (i+n-1 >= m)
# doesn't satisfy
if (i + n - 1 >= m):
return False
# Intialise the new string
Str = ""
# Copy the characters of
# B in str till
# length n
for j in range(n):
Str += (B[i + j])
# Sort the str
Str = sorted(Str)
# Return true if sorted
# of "str" & sorted
# of "A" are equal
if (Str == A):
return True
# Driver Code
if __name__ == '__main__':
# Input str1 and str2
Str1 = "geekforgeeks"
Str2 = "ekegorfkeegsgeek"
# Function return true if
# str1 is shuffled substring
# of str2
a = isShuffledSubstring(Str1, Str2)
# If str1 is subof str2
# print "YES" else print "NO"
if (a):
print("YES")
else:
print("NO")
# This code is contributed by mohit kumar 29
C#
// C# program to check if String
// str1 is subString of str2 or not.
using System;
public class GFG
{
// Function two check String A
// is shuffled subString of B
// or not
static bool isShuffledSubString(String A, String B)
{
int n = A.Length;
int m = B.Length;
// Return false if length of
// String A is greater than
// length of String B
if (n > m)
{
return false;
}
else
{
// Sort String A
A = sort(A);
// Traverse String B
for (int i = 0; i < m; i++)
{
// Return false if (i + n - 1 >= m)
// doesn't satisfy
if (i + n - 1 >= m)
return false;
// Intialise the new String
String str = "";
// Copy the characters of
// String B in str till
// length n
for (int j = 0; j < n; j++)
str += B[i + j];
// Sort the String str
str = sort(str);
// Return true if sorted
// String of "str" & sorted
// String of "A" are equal
if (str.Equals(A))
return true;
}
}
return false;
}
// 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 String.Join("",tempArray);
}
// Driver Code
public static void Main(String[] args)
{
// Input str1 and str2
String str1 = "geekforgeeks";
String str2 = "ekegorfkeegsgeek";
// Function return true if
// str1 is shuffled subString
// of str2
bool a = isShuffledSubString(str1, str2);
// If str1 is subString of str2
// print "YES" else print "NO"
if (a)
Console.Write("YES");
else
Console.Write("NO");
Console.WriteLine();
}
}
// This code is contributed by PrinciRaj1992
Javascript
C++
#include
#include
#define MAX 256
using namespace std;
// This function returns true if contents of arr1[] and arr2[]
// are same, otherwise false.
bool compare(char arr1[], char arr2[])
{
for (int i=0; i
Java
import java.util.*;
class GFG{
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static boolean compare(int []arr1, int []arr2)
{
for(int i = 0; i < 256; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all
// permutations of pat[] in txt[]
static boolean search(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
// countP[]: Store count of all
// characters of pattern
// countTW[]: Store count of
// current window of text
int []countP = new int [256];
int []countTW = new int [256];
for(int i = 0; i < 256; i++)
{
countP[i] = 0;
countTW[i] = 0;
}
for(int i = 0; i < M; i++)
{
(countP[pat.charAt(i)])++;
(countTW[txt.charAt(i)])++;
}
// Traverse through remaining
// characters of pattern
for(int i = M; i < N; i++)
{
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW))
return true;
// Add current character to
// current window
(countTW[txt.charAt(i)])++;
// Remove the first character
// of previous window
countTW[txt.charAt(i - M)]--;
}
// Check for the last window in text
if (compare(countP, countTW))
return true;
return false;
}
// Driver code
public static void main(String[] args)
{
String txt = "BACDGABCDA";
String pat = "ABCD";
if (search(pat, txt))
System.out.println("Yes");
else
System.out.println("NO");
}
}
// This code is contributed by Stream_Cipher
Python3
MAX = 256
# This function returns true if contents
# of arr1[] and arr2[] are same,
# otherwise false.
def compare(arr1, arr2):
global MAX
for i in range(MAX):
if (arr1[i] != arr2[i]):
return False
return True
# This function search for all permutations
# of pat[] in txt[]
def search(pat, txt):
M = len(pat)
N = len(txt)
# countP[]: Store count of all characters
# of pattern
# countTW[]: Store count of current window
# of text
countP = [0 for i in range(MAX)]
countTW = [0 for i in range(MAX)]
for i in range(M):
countP[ord(pat[i])] += 1
countTW[ord(txt[i])] += 1
# Traverse through remaining
# characters of pattern
for i in range(M, N):
# Compare counts of current window
# of text with counts of pattern[]
if (compare(countP, countTW)):
return True
# Add current character
# to current window
countTW[ord(txt[i])] += 1
# Remove the first character
# of previous window
countTW[ord(txt[i - M])] -= 1
# Check for the last window in text
if(compare(countP, countTW)):
return True
return False
# Driver code
txt = "BACDGABCDA"
pat = "ABCD"
if (search(pat, txt)):
print("Yes")
else:
print("No")
# This code is contributed by avanitrachhadiya2155
C#
using System.Collections.Generic;
using System;
class GFG{
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static bool compare(int []arr1, int []arr2)
{
for(int i = 0; i < 256; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all
// permutations of pat[] in txt[]
static bool search(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
// countP[]: Store count of all
// characters of pattern
// countTW[]: Store count of
// current window of text
int []countP = new int [256];
int []countTW = new int [256];
for(int i = 0; i < 256; i++)
{
countP[i] = 0;
countTW[i] = 0;
}
for(int i = 0; i < M; i++)
{
(countP[pat[i]])++;
(countTW[txt[i]])++;
}
// Traverse through remaining
// characters of pattern
for(int i = M; i < N; i++)
{
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW))
return true;
// Add current character to
// current window
(countTW[txt[i]])++;
// Remove the first character
// of previous window
countTW[txt[i - M]]--;
}
// Check for the last window in text
if (compare(countP, countTW))
return true;
return false;
}
// Driver code
public static void Main()
{
string txt = "BACDGABCDA";
string pat = "ABCD";
if (search(pat, txt))
Console.WriteLine("Yes");
else
Console.WriteLine("NO");
}
}
// This code is contributed by Stream_Cipher
YES
时间复杂度: O(m*n*log(n)),其中 n =字符串str1 的长度,m =字符串str2 的长度
辅助空间: O(n)
高效的解决方案:这个问题是字谜搜索的一个更简单的版本。它可以使用字符频率计数在线性时间内解决。
我们可以在字母大小固定的假设下实现 O(n) 时间复杂度,这通常是正确的,因为我们在 ASCII 中最多有 256 个可能的字符。这个想法是使用两个计数数组:
1) 第一个计数数组存储模式中字符的频率。
2) 第二个计数数组存储当前文本窗口中字符的频率。
需要注意的重要一点是,比较两个计数数组的时间复杂度为 O(1),因为其中的元素数量是固定的(与模式和文本大小无关)。以下是该算法的步骤。
1) 在第一个计数数组 countP[] 中存储模式频率的计数。此外,将第一个文本窗口中字符的频率计数存储在数组 countTW[] 中。
2) 现在运行一个从 i = M 到 N-1 的循环。在循环中执行以下操作。
…..a) 如果两个计数数组相同,我们发现了一个事件。
…..b) 在 countTW[] 中增加文本当前字符的计数
…..c) 在 countWT[] 中减少前一个窗口中第一个字符的计数
3)最后一个窗口没有被上面的循环检查,所以明确检查它。
下面是上述算法的实现。
C++
#include
#include
#define MAX 256
using namespace std;
// This function returns true if contents of arr1[] and arr2[]
// are same, otherwise false.
bool compare(char arr1[], char arr2[])
{
for (int i=0; i
Java
import java.util.*;
class GFG{
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static boolean compare(int []arr1, int []arr2)
{
for(int i = 0; i < 256; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all
// permutations of pat[] in txt[]
static boolean search(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
// countP[]: Store count of all
// characters of pattern
// countTW[]: Store count of
// current window of text
int []countP = new int [256];
int []countTW = new int [256];
for(int i = 0; i < 256; i++)
{
countP[i] = 0;
countTW[i] = 0;
}
for(int i = 0; i < M; i++)
{
(countP[pat.charAt(i)])++;
(countTW[txt.charAt(i)])++;
}
// Traverse through remaining
// characters of pattern
for(int i = M; i < N; i++)
{
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW))
return true;
// Add current character to
// current window
(countTW[txt.charAt(i)])++;
// Remove the first character
// of previous window
countTW[txt.charAt(i - M)]--;
}
// Check for the last window in text
if (compare(countP, countTW))
return true;
return false;
}
// Driver code
public static void main(String[] args)
{
String txt = "BACDGABCDA";
String pat = "ABCD";
if (search(pat, txt))
System.out.println("Yes");
else
System.out.println("NO");
}
}
// This code is contributed by Stream_Cipher
蟒蛇3
MAX = 256
# This function returns true if contents
# of arr1[] and arr2[] are same,
# otherwise false.
def compare(arr1, arr2):
global MAX
for i in range(MAX):
if (arr1[i] != arr2[i]):
return False
return True
# This function search for all permutations
# of pat[] in txt[]
def search(pat, txt):
M = len(pat)
N = len(txt)
# countP[]: Store count of all characters
# of pattern
# countTW[]: Store count of current window
# of text
countP = [0 for i in range(MAX)]
countTW = [0 for i in range(MAX)]
for i in range(M):
countP[ord(pat[i])] += 1
countTW[ord(txt[i])] += 1
# Traverse through remaining
# characters of pattern
for i in range(M, N):
# Compare counts of current window
# of text with counts of pattern[]
if (compare(countP, countTW)):
return True
# Add current character
# to current window
countTW[ord(txt[i])] += 1
# Remove the first character
# of previous window
countTW[ord(txt[i - M])] -= 1
# Check for the last window in text
if(compare(countP, countTW)):
return True
return False
# Driver code
txt = "BACDGABCDA"
pat = "ABCD"
if (search(pat, txt)):
print("Yes")
else:
print("No")
# This code is contributed by avanitrachhadiya2155
C#
using System.Collections.Generic;
using System;
class GFG{
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static bool compare(int []arr1, int []arr2)
{
for(int i = 0; i < 256; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all
// permutations of pat[] in txt[]
static bool search(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
// countP[]: Store count of all
// characters of pattern
// countTW[]: Store count of
// current window of text
int []countP = new int [256];
int []countTW = new int [256];
for(int i = 0; i < 256; i++)
{
countP[i] = 0;
countTW[i] = 0;
}
for(int i = 0; i < M; i++)
{
(countP[pat[i]])++;
(countTW[txt[i]])++;
}
// Traverse through remaining
// characters of pattern
for(int i = M; i < N; i++)
{
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW))
return true;
// Add current character to
// current window
(countTW[txt[i]])++;
// Remove the first character
// of previous window
countTW[txt[i - M]]--;
}
// Check for the last window in text
if (compare(countP, countTW))
return true;
return false;
}
// Driver code
public static void Main()
{
string txt = "BACDGABCDA";
string pat = "ABCD";
if (search(pat, txt))
Console.WriteLine("Yes");
else
Console.WriteLine("NO");
}
}
// This code is contributed by Stream_Cipher
Yes
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live