Given an array of strings arr[] and a string str, the task is to print all the strings from the array arr which follow the below conditions:
- The resultant strings shouldn’t contain any consecutive repeating characters.
- The resultant strings should be formed by using only the characters from the string str.
Examples:
Input: arr[] = { “AABCDA”, “ABCDZADC”, “ABCDBCA”, “ABCDABDCA” }, str = “ADCB”
Output: ABCDABDCA ABCDBCA
Input: arr[] = { “A”, “B”, “AB”, “ACB” }, str = “AB”
Output: A B AB
Approach: The idea is to iterate through the array and for every string, check if it contains any consecutive repeating characters and any characters other than those mentioned in the string str. If either of the above conditions passes, then continue to check the next string. Else, print the string.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool check(string s, string str)
{
string chars = s;
set< char > st;
for ( int i = 0; i < str.length(); i++)
st.insert(str[i]);
for ( char c : chars) {
if (st.find(c) == st.end())
return false ;
}
for ( int i = 0; i < chars.length() - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false ;
}
}
return true ;
}
void getStrings(string str, vector<string> arr)
{
for ( int i = 0; i <arr.size(); i++) {
if (check(arr[i], str)) {
cout<<arr[i]<< " " ;
}
}
}
int main()
{
string str = "ABCD" ;
vector<string> arr({ "AABCDA" , "ABCDZADC" , "ABCDBCA" , "ABCDABDCA" });
getStrings(str, arr);
}
|
Java
import java.util.*;
public class GFG {
public static void getStrings(
String str, String[] arr)
{
for ( int i = 0 ; i < arr.length; i++) {
if (check(arr[i], str)) {
System.out.print(arr[i] + " " );
}
}
}
public static boolean check(String s, String str)
{
char [] chars = s.toCharArray();
for ( char c : chars) {
if (!str.contains(String.valueOf(c))) {
return false ;
}
}
for ( int i = 0 ; i < chars.length - 1 ; i++) {
if (chars[i] == chars[i + 1 ]) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String str = "ABCD" ;
String[] arr
= { "AABCDA" , "ABCDZADC" ,
"ABCDBCA" , "ABCDABDCA" };
getStrings(str, arr);
}
}
|
Python3
def getStrings(strr, arr):
for i in range ( len (arr)):
if (check(arr[i], strr)):
print (arr[i],end = " " )
def check(s, strr):
chars = s
for c in chars:
if c not in strr:
return False
for i in range ( len (chars) - 1 ):
if (chars[i] = = chars[i + 1 ]):
return False
return True
strr = "ABCD"
arr = [ "AABCDA" , "ABCDZADC" , "ABCDBCA" , "ABCDABDCA" ]
getStrings(strr, arr)
|
C#
using System;
class GFG {
public static void getStrings(
String str, String[] arr)
{
for ( int i = 0; i < arr.Length; i++) {
if (check(arr[i], str)) {
Console.Write(arr[i] + " " );
}
}
}
public static bool check(String s, String str)
{
char [] chars = s.ToCharArray();
foreach ( char c in chars) {
if (!str.Contains(String.Join( "" ,c))) {
return false ;
}
}
for ( int i = 0; i < chars.Length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String str = "ABCD" ;
String[] arr
= { "AABCDA" , "ABCDZADC" ,
"ABCDBCA" , "ABCDABDCA" };
getStrings(str, arr);
}
}
|
Javascript
<script>
function check(s, str)
{
let chars = s;
let st = new Set();
for (let i = 0; i < str.length; i++)
st.add(str[i]);
for (let c of chars) {
if (!st.has(c))
return false ;
}
for (let i = 0; i < chars.length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return false ;
}
}
return true ;
}
function getStrings(str, arr)
{
for (let i = 0; i < arr.length; i++) {
if (check(arr[i], str)) {
document.write(arr[i] + " " );
}
}
}
let str = "ABCD" ;
let arr = new Array( "AABCDA" , "ABCDZADC" , "ABCDBCA" , "ABCDABDCA" );
getStrings(str, arr);
</script>
|
Output: ABCDBCA ABCDABDCA
Time Complexity: O(m * n), where n is the size of the array (vector) of strings and m is the maximum length of a string in the vector.
Auxiliary Space: O(N), where N is the size of the given string str.