检查给定的字符串是否只能拆分为子序列 ABC
给定一个长度为N的字符串S ,其中字符串的每个字符都是“ A ”、“ B ”或“ C ”。任务是查找是否可以将字符串拆分为子序列“ ABC ”。如果可以拆分,则打印“是”。否则,打印“否”。
例子:
Input: S = “ABABCC”
Output: Yes
Explanation:
One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:
- First form a subsequence “ABC” by taking the character at indices 0, 1, and 4.
- Again form a subsequence “ABC” by taking the character at indices 2, 3, and 5.
Therefore, the string can be split in 2 subsequences of “ABC”.
Input: S = “AABBCC”
Output: Yes
Explanation:
One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:
- First form a subsequence “ABC” by taking the character at indices 0, 2, and 4
- Again form a subsequence “ABC” by taking the character at indices 1, 3, and 5.
Therefore, the string can be split in 2 subsequences of “ABC”.
Input: S = “BAC”
Output: No
方法:可以根据以下观察解决给定的问题:
- It can be observed that if N is not multiple of 3 or count of ‘A‘, ‘B‘, and ‘C‘ are not equal then it will be impossible to split the string satisfying the conditions.
- Also, for every ‘B‘ there must be at least one ‘A‘ to its left and one ‘C’ to its right.
请按照以下步骤解决问题:
- 初始化 3 个整数双端队列,例如A、B和C ,以分别存储字符“ A ”、“ B ”和“ C ”的索引。
- 遍历字符的字符串 S并且在每次迭代中,如果当前字符是“ A ”,则将索引i推入A 。否则,如果当前字符是“ B ”,则将索引i推入B 。否则,将索引i推入C 。
- 如果N不是3的倍数并且字符' A '、' B ' 和 ' C ' 的数量不相等,则打印“No” 。
- 否则,使用变量i迭代双端队列B ,并且在每次迭代中,如果B[i]大于双端队列A的前元素,则弹出双端队列A的前元素。否则打印“否”并返回。
- 现在再次使用变量i反向迭代双端队列B ,并且在每次迭代中,如果B[i]小于双端队列C的最后一个元素,则弹出双端队列C的最后一个元素。否则打印“否”并返回。
- 最后,如果上述情况都不满足,则打印“是”作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the given
// string can be splitted into
// subsequences "ABC"
string check(string S)
{
// Stores the length
// of the string
int N = S.length();
// Stores the indices of 'A'
deque A;
// Stores the indices of 'B'
deque B;
// Stores the indices of 'C'
deque C;
// Traverse the string S
for (int i = 0; i < N; i++) {
// If S[i] is equal to 'A'
if (S[i] == 'A') {
// Push the index i in A
A.push_back(i);
}
// Else if S[i] is equal
// to 'B'
else if (S[i] == 'B') {
// Push the index i in B
B.push_back(i);
}
// Else if S[i] is equal
// to 'C'
else {
// Push the index i in C
C.push_back(i);
}
}
// If N is not multiple of 3 or
// count of characters 'A', 'B'
// and 'C' are not equal
if (N % 3 || A.size() != B.size()
|| A.size() != C.size()) {
// Return "No"
return "No";
}
// Iterate over the deque B
for (int i = 0; i < B.size(); i++) {
// If A is not empty and
// B[i] is greater than
// A[0]
if (!A.empty() && B[i] > A[0]) {
// Removes the front
// element of A
A.pop_front();
}
// Else return "No"
else {
return "No";
}
}
// Iterate over the deque
// B in reverse
for (int i = B.size() - 1; i >= 0; i--) {
// If C is not empty and
// last element of C is
// greater thab B[i]
if (!C.empty() && B[i] < C.back()) {
// Removes the last
// element of C
C.pop_back();
}
// Else return "No"
else {
return "No";
}
}
// If none of the above
// cases satisfy return
//"Yes'
return "Yes";
}
// Driver Code
int main()
{
// Input
string S = "ABABCC";
// Function call
cout << check(S) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check if the given
// string can be splitted into
// subsequences "ABC"
public static String check(String S)
{
// Stores the length
// of the string
int N = S.length();
// Stores the indices of 'A'
Deque A = new LinkedList();
// Stores the indices of 'B'
Deque B = new LinkedList();
// Stores the indices of 'C'
Deque C = new LinkedList();
// Traverse the string S
for(int i = 0; i < N; i++)
{
// If S[i] is equal to 'A'
if (S.charAt(i) == 'A')
{
// Push the index i in A
A.addLast(i);
}
// Else if S[i] is equal
// to 'B'
else if (S.charAt(i) == 'B')
{
// Push the index i in B
B.addLast(i);
}
// Else if S[i] is equal
// to 'C'
else
{
// Push the index i in C
C.addLast(i);
}
}
// If N is not multiple of 3 or
// count of characters 'A', 'B'
// and 'C' are not equal
if (N % 3 > 0 || A.size() != B.size() ||
A.size() != C.size())
{
// Return "No"
return "No";
}
// Iterate over the deque B
for(Iterator itr = B.iterator(); itr.hasNext();)
{
Integer b = (Integer)itr.next();
// If A is not empty and
// curr B is greater than
// A[0]
if (A.size() > 0 && b > A.getFirst())
{
// Removes the front
// element of A
A.pop();
}
// Else return "No"
else
{
return "No";
}
}
// Iterate over the deque
// B in reverse
for(Iterator itr = B.descendingIterator();
itr.hasNext();)
{
Integer b = (Integer)itr.next();
// If C is not empty and
// last element of C is
// greater thab B[i]
if (C.size() > 0 && b < C.getLast())
{
// Removes the last
// element of C
C.pollLast();
}
// Else return "No"
else
{
return "No";
}
}
// If none of the above
// cases satisfy return
//"Yes'
return "Yes";
}
// Driver code
public static void main(String[] args)
{
// Input
String S = "ABABCC";
// Function call
System.out.println(check(S));
}
}
// This code is contributed by Manu Pathria
Python3
# Python3 program for the above approach
from collections import deque
# Function to check if the given
# can be splitted into
# subsequences "ABC"
def check(S):
# Stores the length
# of the string
N = len(S)
# Stores the indices of 'A'
A = deque()
# Stores the indices of 'B'
B = deque()
# Stores the indices of 'C'
C = deque()
# Traverse the S
for i in range(N):
# If S[i] is equal to 'A'
if (S[i] == 'A'):
# Push the index i in A
A.append(i)
# Else if S[i] is equal
# to 'B'
elif (S[i] == 'B'):
# Push the index i in B
B.append(i)
# Else if S[i] is equal
# to 'C'
else:
# Push the index i in C
C.append(i)
# If N is not multiple of 3 or
# count of characters 'A', 'B'
# and 'C' are not equal
if (N % 3 or len(A) != len(B) or
len(A) != len(C)):
# Return "No"
return "No"
# Iterate over the deque B
for i in range(len(B)):
# If A is not empty and
# B[i] is greater than
# A[0]
if (len(A) > 0 and B[i] > A[0]):
# Removes the front
# element of A
A.popleft()
# Else return "No"
else:
return "No"
# Iterate over the deque
# B in reverse
for i in range(len(B) - 1, -1, -1):
# If C is not empty and
# last element of C is
# greater thab B[i]
if (len(C) > 0 and B[i] < C[-1]):
# Removes the last
# element of C
C.popleft()
# Else return "No"
else:
return "No"
# If none of the above
# cases satisfy return
# "Yes'
return "Yes"
# Driver Code
if __name__ == '__main__':
# Input
S = "ABABCC"
# Function call
print(check(S))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public static class GFG {
public static IEnumerable Reverse(this LinkedList list) {
var el = list.Last;
while (el != null) {
yield return el.Value;
el = el.Previous;
}
}
// Function to check if the given
// string can be splitted into
// subsequences "ABC"
public static string check(string S)
{
// Stores the length
// of the string
int N = S.Length;
// Stores the indices of 'A'
LinkedList A = new LinkedList();
// Stores the indices of 'B'
LinkedList B = new LinkedList();
// Stores the indices of 'C'
LinkedList C = new LinkedList();
// Traverse the string S
for(int i = 0; i < N; i++)
{
// If S[i] is equal to 'A'
if (S[i] == 'A')
{
// Push the index i in A
A.AddLast(i);
}
// Else if S[i] is equal
// to 'B'
else if (S[i] == 'B')
{
// Push the index i in B
B.AddLast(i);
}
// Else if S[i] is equal
// to 'C'
else
{
// Push the index i in C
C.AddLast(i);
}
}
// If N is not multiple of 3 or
// count of characters 'A', 'B'
// and 'C' are not equal
if (N % 3 > 0 || A.Count != B.Count ||
A.Count != C.Count)
{
// Return "No"
return "No";
}
// Iterate over the deque B
foreach(var itr in B)
{
int b = itr;
// If A is not empty and
// curr B is greater than
// A[0]
if (A.Count > 0 && b > A.First.Value)
{
// Removes the front
// element of A
A.RemoveFirst();
}
// Else return "No"
else
{
return "No";
}
}
// Iterate over the deque
// B in reverse
foreach(var itr in B.Reverse())
{
int b = itr;
// If C is not empty and
// last element of C is
// greater thab B[i]
if (C.Count > 0 && b < C.Last.Value)
{
// Removes the last
// element of C
C.RemoveLast();
}
// Else return "No"
else
{
return "No";
}
}
// If none of the above
// cases satisfy return
//"Yes'
return "Yes";
}
// Driver code
static public void Main (){
// Input
string S = "ABABCC";
// Function call
Console.Write(check(S));
}
}
// This Code is contributed by ShubhamSingh10
输出
Yes
时间复杂度: O(N)
辅助空间: O(N)