给定一个字符串数组arr []和一个字符串str ,任务是从数组arr打印所有符合以下条件的字符串:
- 结果字符串不应包含任何连续的重复字符。
- 结果字符串应仅使用字符串str中的字符组成。
例子:
Input: arr[] = { “AABCDA”, “ABCDZADC”, “ABCDBCA”, “ABCDABDCA” }, str = “ADCB”
Output: ABCDABDCA ABCDBCA
Input: arr[] = { “A”, “B”, “AB”, “ACB” }, str = “AB”
Output: A B AB
方法:想法是遍历数组,对于每个字符串,检查它是否包含任何连续的重复字符以及字符串str中提到的字符以外的任何其他字符。如果以上条件之一通过,则继续检查下一个字符串。否则,打印字符串。
下面是上述方法的实现:
C++
// CPP implementation of the above approach
#include
using namespace std;
// Function to check whether the string contains
// any consecutive repetitive characters
// and any characters other than those in str
bool check(string s, string str)
{
string chars = s;
set st;
// Valid characters check
for(int i = 0; i < str.length(); i++)
st.insert(str[i]);
for (char c : chars) {
if(st.find(c) == st.end())
return false;
}
// Nonrepetitive check
for (int i = 0; i < chars.length() - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false;
}
}
return true;
}
// Function to print the strings which
// satisfy the mentioned conditions
void getStrings(string str, vector arr)
{
// Iterate through all the strings
// in the array.
for (int i = 0; i arr({"AABCDA", "ABCDZADC","ABCDBCA", "ABCDABDCA"});
getStrings(str, arr);
}
// This code is contributed by Surendra_Gangwar
Java
// Java implementation of the above approach
import java.util.*;
public class GFG {
// Function to print the strings which
// satisfy the mentioned conditions
public static void getStrings(
String str, String[] arr)
{
// Iterate through all the strings
// in the array.
for (int i = 0; i < arr.length; i++) {
// check function to check the
// conditions for every string
if (check(arr[i], str)) {
System.out.print(arr[i] + " ");
}
}
}
// Function to check whether the string contains
// any consecutive repetitive characters
// and any characters other than those in str
public static boolean check(String s, String str)
{
char[] chars = s.toCharArray();
// Valid characters check
for (char c : chars) {
if (!str.contains(String.valueOf(c))) {
return false;
}
}
// Nonrepetitive check
for (int i = 0; i < chars.length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false;
}
}
return true;
}
// Driver code
public static void main(String[] args)
{
String str = "ABCD";
String[] arr
= { "AABCDA", "ABCDZADC",
"ABCDBCA", "ABCDABDCA" };
getStrings(str, arr);
}
}
Python3
# Python3 implementation of the above approach
# Function to print the strings which
# satisfy the mentioned conditions
def getStrings(strr, arr):
# Iterate through all the strings
# in the array.
for i in range(len(arr)):
# check function to check the
# conditions for every string
if (check(arr[i], strr)):
print(arr[i],end=" ")
# Function to check whether the string contains
# any consecutive repetitive characters
# and any characters other than those in str
def check(s, strr):
chars = s
# Valid characters check
for c in chars:
if c not in strr:
return False
# Nonrepetitive check
for i in range(len(chars)-1):
if (chars[i] == chars[i + 1]):
return False
return True
# Driver code
strr = "ABCD"
arr = ["AABCDA", "ABCDZADC","ABCDBCA", "ABCDABDCA"]
getStrings(strr, arr)
# This code is contributed by shubhamsingh10
C#
// C# implementation of the above approach
using System;
class GFG {
// Function to print the strings which
// satisfy the mentioned conditions
public static void getStrings(
String str, String[] arr)
{
// Iterate through all the strings
// in the array.
for (int i = 0; i < arr.Length; i++) {
// check function to check the
// conditions for every string
if (check(arr[i], str)) {
Console.Write(arr[i] + " ");
}
}
}
// Function to check whether the string contains
// any consecutive repetitive characters
// and any characters other than those in str
public static bool check(String s, String str)
{
char[] chars = s.ToCharArray();
// Valid characters check
foreach (char c in chars) {
if (!str.Contains(String.Join("",c))) {
return false;
}
}
// Nonrepetitive check
for (int i = 0; i < chars.Length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false;
}
}
return true;
}
// Driver code
public static void Main(String[] args)
{
String str = "ABCD";
String[] arr
= { "AABCDA", "ABCDZADC",
"ABCDBCA", "ABCDABDCA" };
getStrings(str, arr);
}
}
// This code is contributed by 29AjayKumar
输出:
ABCDBCA ABCDABDCA