从给定的字符串中删除“b”和“ac”
给定一个字符串,消除字符串中的所有“b”和“ac”,你必须就地替换它们,并且你只能迭代字符串一次。 (来源谷歌面试题)
例子:
acbac ==> ""
aaac ==> aa
ababac ==> aa
bbbbd ==> d
我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。
这两个条件是:
1.过滤所有'b'和'ac'应该是单程的
2.不允许有多余的空间。
方法是使用两个索引变量 i 和 j。我们使用'i'在字符串中前进,并使用索引j添加字符,除了'b'和'ac'。这里的诀窍是如何在“c”之前跟踪“a”。一个有趣的方法是使用两个状态机。当前一个字符为“a”时,状态保持为二,否则状态为一。
1)如果状态为 ONE,则如果以下条件之一为真,则不要将当前字符复制到输出
... a)当前字符是 'b'(我们需要删除 'b')
... b)当前字符是 'a'(下一个字符可能是 'c')
2)如果 state 是 TWO 并且当前字符不是 'c',我们首先需要确保我们复制了前一个字符'a'。然后我们检查当前字符,如果当前字符不是'b'而不是'a',那么我们将它复制到输出。
C++
// A C++ program to remove "b" and 'ac' from input string
#include
using namespace std;
#define ONE 1
#define TWO 2
// The main function that removes occurrences of "a" and "bc"
// in input string
void stringFilter(char *str)
{
// state is initially ONE (The previous character is not a)
int state = ONE;
// i and j are index variables, i is used to read next
// character of input string, j is used for indexes of output
// string (modified input string)
int j = 0;
// Process all characters of input string one by one
for (int i = 0; str[i] != '\0'; i++)
{
/* If state is ONE, then do NOT copy the current character
to output if one of the following conditions is true
...a) Current character is 'b' (We need to remove 'b')
...b) Current character is 'a' (Next character may be 'c') */
if (state == ONE && str[i] != 'a' && str[i] != 'b')
{
str[j] = str[i];
j++;
}
// If state is TWO and current character is not 'c' (other-
// wise we ignore both previous and current characters)
if (state == TWO && str[i] != 'c')
{
// First copy the previous 'a'
str[j] = 'a';
j++;
// Then copy the current character if it is not 'a'
// and 'b'
if (str[i] != 'a' && str[i] != 'b')
{
str[j] = str[i];
j++;
}
}
// Change state according to current character
state = (str[i] == 'a')? TWO: ONE;
}
// If last character was 'a', copy it to output
if (state == TWO)
{
str[j] = 'a';
j++;
}
// Set the string terminator
str[j] = '\0';
}
/* Driver program to check above functions */
int main()
{
char str1[] = "ad";
stringFilter(str1);
cout << str1 << endl;
char str2[] = "acbac";
stringFilter(str2);
cout << str2 << endl;
char str3[] = "aaac";
stringFilter(str3);
cout << str3 << endl;
char str4[] = "react";
stringFilter(str4);
cout << str4 << endl;
char str5[] = "aa";
stringFilter(str5);
cout << str5 << endl;
char str6[] = "ababaac";
stringFilter(str6);
cout << str6 << endl;
return 0;
}
Java
// A Java program to remove "b" and
// 'ac' from input String
import java.util.*;
class GFG
{
static final int ONE = 1;
static final int TWO = 2;
// The main function that removes occurrences
// of "a" and "bc" in input String
static char[] StringFilter(char []str)
{
// state is initially ONE
// (The previous character is not a)
int state = ONE;
// i and j are index variables,
// i is used to read next
// character of input String,
// j is used for indexes of output
// String (modified input String)
int j = 0;
// Process all characters of
// input String one by one
for (int i = 0; i < str.length; i++)
{
/* If state is ONE, then do NOT copy
the current character to output if
one of the following conditions is true
...a) Current character is 'b'
(We need to remove 'b')
...b) Current character is 'a'
(Next character may be 'c') */
if (state == ONE && str[i] != 'a' &&
str[i] != 'b')
{
str[j] = str[i];
j++;
}
// If state is TWO and current character
// is not 'c' (otherwise we ignore both
// previous and current characters)
if (state == TWO && str[i] != 'c')
{
// First copy the previous 'a'
str[j] = 'a';
j++;
// Then copy the current character
// if it is not 'a' and 'b'
if (str[i] != 'a' && str[i] != 'b')
{
str[j] = str[i];
j++;
}
}
// Change state according to current character
state = (str[i] == 'a') ? TWO : ONE;
}
// If last character was 'a',
// copy it to output
if (state == TWO)
{
str[j] = 'a';
j++;
}
return Arrays.copyOfRange(str, 0, j);
}
// Driver Code
public static void main(String[] args)
{
char str1[] = "ad".toCharArray();
str1 = StringFilter(str1);
System.out.print(String.valueOf(str1) + "\n");
char str2[] = "acbac".toCharArray();
str2 = StringFilter(str2);
System.out.print(String.valueOf(str2) + "\n");
char str3[] = "aaac".toCharArray();
str3 = StringFilter(str3);
System.out.print(String.valueOf(str3) + "\n");
char str4[] = "react".toCharArray();
str4 = StringFilter(str4);
System.out.print(String.valueOf(str4) + "\n");
char str5[] = "aa".toCharArray();
str5 = StringFilter(str5);
System.out.print(String.valueOf(str5) + "\n");
char str6[] = "ababaac".toCharArray();
str6 = StringFilter(str6);
System.out.print(String.valueOf(str6) + "\n");
}
}
// This code is contributed by 29AjayKumar
Python
# A Python program to remove "b" and 'ac' from input string
ONE = 1
TWO = 2
# Utility function to convert string to list
def toList(string):
l = []
for x in string:
l.append(x)
return l
# Utility function to convert list to string
def toString(l):
return ''.join(l)
# The main function that removes occurrences of "a" and "bc"
# in input string
def stringFilter(string):
# state is initially ONE (The previous character is not a)
state = ONE
# i and j are index variables, i is used to read next
# character of input string, j is used for indexes of
# output string (modified input string)
j = 0
# Process all characters of input string one by one
for i in xrange(len(string)):
# If state is ONE, then do NOT copy the current character
# to output if one of the following conditions is true
# ...a) Current character is 'b' (We need to remove 'b')
# ...b) Current character is 'a' (Next character may be 'c')
if state == ONE and string[i] != 'a' and string[i] != 'b':
string[j] = string[i]
j += 1
# If state is TWO and current character is not 'c' (other-
# wise we ignore both previous and current characters)
if state == TWO and string[i] != 'c':
# First copy the previous 'a'
string[j] = 'a'
j += 1
# Then copy the current character if it is not 'a' and 'b'
if string[i] != 'a' and string[i] != 'b':
string[j] = string[i]
j += 1
# Change state according to current character
state = TWO if string[i] == 'a' else ONE
# If last character was 'a', copy it to output
if state == TWO:
string[j] = 'a'
j += 1
return toString(string[:j])
# Driver program
string1 = stringFilter(toList("ad"))
print string1
string2 = stringFilter(toList("acbac"))
print string2
string3 = stringFilter(toList("aaac"))
print string3
string4 = stringFilter(toList("react"))
print string4
string5 = stringFilter(toList("aa"))
print string5
string6 = stringFilter(toList("ababaac"))
print string6
# This code is contributed by BHAVYA JAIN
C#
// A C# program to remove "b" and
// 'ac' from input String
using System;
class GFG
{
static readonly int ONE = 1;
static readonly int TWO = 2;
// The main function that removes occurrences
// of "a" and "bc" in input String
static char[] StringFilter(char []str)
{
// state is initially ONE
// (The previous character is not a)
int state = ONE;
// i and j are index variables,
// i is used to read next
// character of input String,
// j is used for indexes of output
// String (modified input String)
int j = 0;
// Process all characters of
// input String one by one
for (int i = 0; i < str.Length; i++)
{
/* If state is ONE, then do NOT copy
the current character to output if
one of the following conditions is true
...a) Current character is 'b'
(We need to remove 'b')
...b) Current character is 'a'
(Next character may be 'c') */
if (state == ONE && str[i] != 'a' &&
str[i] != 'b')
{
str[j] = str[i];
j++;
}
// If state is TWO and current character
// is not 'c' (otherwise we ignore both
// previous and current characters)
if (state == TWO && str[i] != 'c')
{
// First copy the previous 'a'
str[j] = 'a';
j++;
// Then copy the current character
// if it is not 'a' and 'b'
if (str[i] != 'a' && str[i] != 'b')
{
str[j] = str[i];
j++;
}
}
// Change state according to
// current character
state = (str[i] == 'a') ? TWO : ONE;
}
// If last character was 'a',
// copy it to output
if (state == TWO)
{
str[j] = 'a';
j++;
}
return String.Join("", str).
Substring(0, j).ToCharArray();
}
// Driver Code
public static void Main(String[] args)
{
char []str1 = "ad".ToCharArray();
str1 = StringFilter(str1);
Console.Write(String.Join("", str1) + "\n");
char []str2 = "acbac".ToCharArray();
str2 = StringFilter(str2);
Console.Write(String.Join("", str2) + "\n");
char []str3 = "aaac".ToCharArray();
str3 = StringFilter(str3);
Console.Write(String.Join("", str3) + "\n");
char []str4 = "react".ToCharArray();
str4 = StringFilter(str4);
Console.Write(String.Join("", str4) + "\n");
char []str5 = "aa".ToCharArray();
str5 = StringFilter(str5);
Console.Write(String.Join("", str5) + "\n");
char []str6 = "ababaac".ToCharArray();
str6 = StringFilter(str6);
Console.Write(String.Join("", str6) + "\n");
}
}
// This code is contributed by Rajput-Ji
C++
// // A C++ program to remove "b" and 'ac' from input string
#include
using namespace std;
void stringFilter(char *str)
{
int n = strlen(str);
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a')
i--,j++;
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
str[++i] = '\0';
}
// Driver program to test above function
int main()
{
char str1[] = "ad";
cout << "Input => " << str1 << "\nOutput => ";
stringFilter(str1);
cout << str1 << endl << endl;
char str2[] = "acbac";
cout << "Input => " << str2 << "\nOutput => ";
stringFilter(str2);
cout << str2 << endl << endl;
char str3[] = "aaac";
cout << "Input => " << str3 << "\nOutput => ";
stringFilter(str3);
cout << str3 << endl << endl;
char str4[] = "react";
cout << "Input => " << str4 << "\nOutput => ";
stringFilter(str4);
cout << str4 << endl << endl;
char str5[] = "aa";
cout << "Input => " << str5 << "\nOutput => ";
stringFilter(str5);
cout << str5 << endl << endl;
char str6[] = "ababaac";
cout << "Input => " << str6 << "\nOutput => ";
stringFilter(str6);
cout << str6 << endl << endl;
char str[] = "abc";
cout << "Input => " << str << "\nOutput => ";
stringFilter(str);
cout << str << endl << endl;
return 0;
}
Java
// A Java program to remove "b" and 'ac' from input string
class GfG {
// The main function that removes occurrences of "a" and "bc"
// in input string
static void stringFilter(char str[])
{
int n = str.length;
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a') {
i--;
j++;
}
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
System.out.println(new String(str).substring(0,i+1));
}
/* Driver program to check above functions */
public static void main(String[] args)
{
String str1 = "ad";
stringFilter(str1.toCharArray());
String str2 = "acbac";
stringFilter(str2.toCharArray());
String str3 = "aaac";
stringFilter(str3.toCharArray());
String str4 = "react";
stringFilter(str4.toCharArray());
String str5 = "aa";
stringFilter(str5.toCharArray());
String str6 = "ababaac";
stringFilter(str6.toCharArray());
}
}
Python
# A Python program to remove "b" and 'ac' from input string
# Utility function to convert string to list
def toList(string):
l = []
for x in string:
l.append(x)
return l
# Utility function to convert list to string
def toString(l):
return ''.join(l)
def stringFilter(string):
# length of string
n = len(string)
i = -1
j = 0
while j < n:
# Check if current and next character forms ac
if j < n-1 and string[j] == 'a' and string[j+1] == 'c':
j += 2
# If current character is b
elif string[j] == 'b':
j += 1
# if current char is 'c && last char in output
# is 'a' so delete both
elif i >= 0 and string[j] == 'c' and string[i] == 'a':
i -= 1
j += 1
# Else copy curr char to output string
else:
i += 1
string[i] = string[j]
j += 1
i += 1
return toString(string[:i])
# Driver program
string1 = "ad"
print "Input => " + string1 + "\nOutput => ",
print stringFilter(toList(string1)) + "\n"
string2 = "acbac"
print "Input => " + string2 + "\nOutput => ",
print stringFilter(toList(string2)) + "\n"
string3 = "aaac"
print "Input => " + string3 + "\nOutput => ",
print stringFilter(toList(string3)) + "\n"
string4 = "react"
print "Input => " + string4 + "\nOutput => ",
print stringFilter(toList(string4)) + "\n"
string5 = "aa"
print "Input => " + string5 + "\nOutput => ",
print stringFilter(toList(string5)) + "\n"
string6 = "ababaac"
print "Input => " + string6 + "\nOutput => ",
print stringFilter(toList(string6)) + "\n"
string7 = "abc"
print "Input => " + string7 + "\nOutput => ",
print stringFilter(toList(string7)) + "\n"
# This code is contributed by BHAVYA JAIN
C#
// C# program to remove "b" and 'ac' from input string
using System;
class GfG
{
// The main function that removes occurrences of
// "a" and "bc" in input string
static void stringFilter(char []str)
{
int n = str.Length;
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a')
{
i--;
j++;
}
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
Console.WriteLine(new String(str));
}
// Driver Code
public static void Main()
{
String str1 = "ad";
stringFilter(str1.ToCharArray());
String str2 = "acbac";
stringFilter(str2.ToCharArray());
String str3 = "aaac";
stringFilter(str3.ToCharArray());
String str4 = "react";
stringFilter(str4.ToCharArray());
String str5 = "aa";
stringFilter(str5.ToCharArray());
String str6 = "ababaac";
stringFilter(str6.ToCharArray());
}
}
// This code is contributed by
//PrinciRaj1992
输出:
ad
aa
ret
aa
aaa
上述问题的扩展,我们根本不希望在输出中出现“ac”:
上面的代码看起来很好,似乎可以处理所有情况,但是如果输入字符串是“aacacc”,上面的代码会产生输出为“ac”,这看起来是正确的,因为它删除了连续出现的“a”和“c”。如果要求在输出字符串中根本没有“ac”怎么办。我们可以修改上面的程序,为输入“aacacc”产生空字符串输出,当输入为“abcaaccd”时产生输出为“d”?事实证明,它也可以在给定的限制下完成。这个想法很简单。我们需要在上述程序的 for 循环中添加以下行。
if (j>1 && str[j-2] == 'a' && str[j-1] =='c')
j = j-2;
有关修改程序的不同测试用例,请参见此内容。
原始问题的更简单解决方案:
C++
// // A C++ program to remove "b" and 'ac' from input string
#include
using namespace std;
void stringFilter(char *str)
{
int n = strlen(str);
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a')
i--,j++;
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
str[++i] = '\0';
}
// Driver program to test above function
int main()
{
char str1[] = "ad";
cout << "Input => " << str1 << "\nOutput => ";
stringFilter(str1);
cout << str1 << endl << endl;
char str2[] = "acbac";
cout << "Input => " << str2 << "\nOutput => ";
stringFilter(str2);
cout << str2 << endl << endl;
char str3[] = "aaac";
cout << "Input => " << str3 << "\nOutput => ";
stringFilter(str3);
cout << str3 << endl << endl;
char str4[] = "react";
cout << "Input => " << str4 << "\nOutput => ";
stringFilter(str4);
cout << str4 << endl << endl;
char str5[] = "aa";
cout << "Input => " << str5 << "\nOutput => ";
stringFilter(str5);
cout << str5 << endl << endl;
char str6[] = "ababaac";
cout << "Input => " << str6 << "\nOutput => ";
stringFilter(str6);
cout << str6 << endl << endl;
char str[] = "abc";
cout << "Input => " << str << "\nOutput => ";
stringFilter(str);
cout << str << endl << endl;
return 0;
}
Java
// A Java program to remove "b" and 'ac' from input string
class GfG {
// The main function that removes occurrences of "a" and "bc"
// in input string
static void stringFilter(char str[])
{
int n = str.length;
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a') {
i--;
j++;
}
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
System.out.println(new String(str).substring(0,i+1));
}
/* Driver program to check above functions */
public static void main(String[] args)
{
String str1 = "ad";
stringFilter(str1.toCharArray());
String str2 = "acbac";
stringFilter(str2.toCharArray());
String str3 = "aaac";
stringFilter(str3.toCharArray());
String str4 = "react";
stringFilter(str4.toCharArray());
String str5 = "aa";
stringFilter(str5.toCharArray());
String str6 = "ababaac";
stringFilter(str6.toCharArray());
}
}
Python
# A Python program to remove "b" and 'ac' from input string
# Utility function to convert string to list
def toList(string):
l = []
for x in string:
l.append(x)
return l
# Utility function to convert list to string
def toString(l):
return ''.join(l)
def stringFilter(string):
# length of string
n = len(string)
i = -1
j = 0
while j < n:
# Check if current and next character forms ac
if j < n-1 and string[j] == 'a' and string[j+1] == 'c':
j += 2
# If current character is b
elif string[j] == 'b':
j += 1
# if current char is 'c && last char in output
# is 'a' so delete both
elif i >= 0 and string[j] == 'c' and string[i] == 'a':
i -= 1
j += 1
# Else copy curr char to output string
else:
i += 1
string[i] = string[j]
j += 1
i += 1
return toString(string[:i])
# Driver program
string1 = "ad"
print "Input => " + string1 + "\nOutput => ",
print stringFilter(toList(string1)) + "\n"
string2 = "acbac"
print "Input => " + string2 + "\nOutput => ",
print stringFilter(toList(string2)) + "\n"
string3 = "aaac"
print "Input => " + string3 + "\nOutput => ",
print stringFilter(toList(string3)) + "\n"
string4 = "react"
print "Input => " + string4 + "\nOutput => ",
print stringFilter(toList(string4)) + "\n"
string5 = "aa"
print "Input => " + string5 + "\nOutput => ",
print stringFilter(toList(string5)) + "\n"
string6 = "ababaac"
print "Input => " + string6 + "\nOutput => ",
print stringFilter(toList(string6)) + "\n"
string7 = "abc"
print "Input => " + string7 + "\nOutput => ",
print stringFilter(toList(string7)) + "\n"
# This code is contributed by BHAVYA JAIN
C#
// C# program to remove "b" and 'ac' from input string
using System;
class GfG
{
// The main function that removes occurrences of
// "a" and "bc" in input string
static void stringFilter(char []str)
{
int n = str.Length;
int i = -1; // previous character
int j = 0; // current character
while (j < n)
{
/* check if current and next character forms ac */
if (j < n-1 && str[j] == 'a' && str[j+1] == 'c')
j += 2;
/* if current character is b */
else if (str[j] == 'b')
j++;
/* if current char is 'c && last char in output
is 'a' so delete both */
else if (i >= 0 && str[j] == 'c' && str[i] == 'a')
{
i--;
j++;
}
/* else copy curr char to output string */
else
str[++i] = str[j++];
}
Console.WriteLine(new String(str));
}
// Driver Code
public static void Main()
{
String str1 = "ad";
stringFilter(str1.ToCharArray());
String str2 = "acbac";
stringFilter(str2.ToCharArray());
String str3 = "aaac";
stringFilter(str3.ToCharArray());
String str4 = "react";
stringFilter(str4.ToCharArray());
String str5 = "aa";
stringFilter(str5.ToCharArray());
String str6 = "ababaac";
stringFilter(str6.ToCharArray());
}
}
// This code is contributed by
//PrinciRaj1992
输出:
Input => ad
Output => ad
Input => acbac
Output =>
Input => aaac
Output => aa
Input => react
Output => ret
Input => aa
Output => aa
Input => ababaac
Output => aaa
Input => abc
Output =>
感谢 Gaurav Ahirwar 提出了这个更简单的解决方案。