Strings formed from given characters without any consecutive repeating characters

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:

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 condition passes, then continue to check the next string. Else, print the string.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of the above approach
#include<bits/stdc++.h>
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<char> 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<string> arr)
{
    // Iterate through all the strings
    // in the array.
    for (int i = 0; i <arr.size(); i++) {
  
        // check function to check the
        // conditions for every string
        if (check(arr[i], str)) {
            cout<<arr[i]<<" ";
        }
    }
}
  
// Driver code
int main()
{
    string str = "ABCD";
    vector<string> arr({"AABCDA", "ABCDZADC","ABCDBCA", "ABCDABDCA"});
    getStrings(str, arr);
}
  
// This code is contributed by Surendra_Gangwar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
ABCDBCA ABCDABDCA

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :