检查是否可以使用重复插入另一个字符串S2 来形成字符串S1
给定两个由唯一字符组成的字符串S1和S2 ,任务是检查 S1 是否可以通过重复插入字符串S2形成。
Input: S1 = “aabb”, S2 = “ab”
Output: Yes
Explanation: the mentioned string can be obtained after series of moves:
- Insert string “ab” in an empty string. Current string will be “ab“
- insert “ab” after “a”. The final string will be “aabb”
Input: S1 = “ababcd”, S2 = “abc”
Output: No
Explanation: It is not possible to obtain above string with any series of moves.
方法:给定的问题可以使用堆栈数据结构来解决。想法是将 S1 的字符插入堆栈,直到找到 S2 的最后一个字符。然后从堆栈中弹出长度(S2)的字符并与S2进行比较。如果不相同,则停止并返回 false。否则重复该过程直到 S1 变空。
以下是上述方法的步骤:
- 首先,检查以下情况,如果发现为真则返回假:
- S1 中的唯一字符数必须与 S2 相同
- 字符串S1 的长度必须是 S2 的倍数
- 保持一个 堆 对于所有字符
- 遍历字符串S1并将字符压入堆栈
- 如果当前字符是字符串S2的最后一个字符,则匹配堆栈左侧的所有字符
- 如果任何位置的堆栈为空或字符不匹配,则返回 False
- 对字符串进行完整迭代后,检查堆栈是否为空。如果堆栈不为空,则返回 false 否则返回 true
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to check a valid insertion
bool validInsertionstring(string S1, string S2)
{
// Store the size of string
int N = S1.length();
int M = S2.length();
// Maintain a stack for characters
stack st;
// Iterate through the string
for (int i = 0; i < N; i++) {
// push the current character
// on top of the stack
st.push(S1[i]);
// If the current character is the
// last character of string S2 then
// pop characters until S2 is not formed
if (S1[i] == S2[M - 1]) {
// index of last character of the string S2
int idx = M - 1;
// pop characters till 0-th index
while (idx >= 0) {
if (st.empty()) {
return false;
}
char c = st.top();
st.pop();
if (c != S2[idx]) {
return false;
}
idx--;
}
}
}
// Check if stack in non-empty
if (!st.empty()) {
return false;
}
else {
return true;
}
}
// Driver Code
int main()
{
string S1 = "aabb";
string S2 = "ab";
validInsertionstring(S1, S2) ? cout << "Yes\n"
: cout << "No\n";
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to check a valid insertion
static boolean validInsertionstring(String S1,
String S2)
{
// Store the size of string
int N = S1.length();
int M = S2.length();
// Maintain a stack for characters
Stack st = new Stack<>();
// Iterate through the string
for (int i = 0; i < N; i++) {
// push the current character
// on top of the stack
st.push(S1.charAt(i));
// If the current character is the
// last character of string S2 then
// pop characters until S2 is not formed
if (S1.charAt(i) == S2.charAt(M - 1)) {
// index of last character of the string S2
int idx = M - 1;
// pop characters till 0-th index
while (idx >= 0) {
if (st.size() == 0) {
return false;
}
char c = st.peek();
st.pop();
if (c != S2.charAt(idx)) {
return false;
}
idx--;
}
}
}
// Check if stack in non-empty
if (st.size() > 0) {
return false;
}
else {
return true;
}
}
// Driver code
public static void main(String[] args)
{
String S1 = "aabb";
String S2 = "ab";
if (validInsertionstring(S1, S2) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 implementation for the above approach
# Function to check a valid insertion
def validInsertionstring(S1, S2):
# Store the size of string
N = len(S1)
M = len(S2)
# Maintain a stack for characters
st = []
# Iterate through the string
for i in range(N):
# push the current character
# on top of the stack
st.append(S1[i])
# If the current character is the
# last character of string S2 then
# pop characters until S2 is not formed
if (S1[i] == S2[M - 1]):
# index of last character of the string S2
idx = M - 1
# pop characters till 0-th index
while (idx >= 0):
if (len(st) == 0):
return False
c = st[-1]
st.pop()
if (c != S2[idx]):
return False
idx-=1
# Check if stack in non-empty
if (len(st) != 0):
return False
else:
return True
S1 = "aabb"
S2 = "ab"
if validInsertionstring(S1, S2):
print("Yes")
else:
print("No")
# This code is contributed by divyeshrabadiya07.
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check a valid insertion
static bool validInsertionstring(string S1, string S2)
{
// Store the size of string
int N = S1.Length;
int M = S2.Length;
// Maintain a stack for characters
Stack st = new Stack();
// Iterate through the string
for (int i = 0; i < N; i++) {
// push the current character
// on top of the stack
st.Push(S1[i]);
// If the current character is the
// last character of string S2 then
// pop characters until S2 is not formed
if (S1[i] == S2[M - 1]) {
// index of last character of the string S2
int idx = M - 1;
// pop characters till 0-th index
while (idx >= 0) {
if (st.Count==0) {
return false;
}
char c = st.Peek();
st.Pop();
if (c != S2[idx]) {
return false;
}
idx--;
}
}
}
// Check if stack in non-empty
if (st.Count > 0) {
return false;
}
else {
return true;
}
}
// Driver Code
public static void Main()
{
string S1 = "aabb";
string S2 = "ab";
if(validInsertionstring(S1, S2)==true)
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
Yes
时间复杂度: O(N*M)
辅助空间: O(N)