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 ABCDBCAInput: 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++
// 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 |
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 |
Javascript
<script> // Javascript implementation of the above approach // Function to check whether the string contains // any consecutive repetitive characters // and any characters other than those in str function check(s, str) { let chars = s; let st = new Set(); // Valid characters check for (let i = 0; i < str.length; i++) st.add(str[i]); for (let c of chars) { if (!st.has(c)) return false ; } // Nonrepetitive check for (let 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 function getStrings(str, arr) { // Iterate through all the strings // in the array. for (let i = 0; i < arr.length; i++) { // check function to check the // conditions for every string if (check(arr[i], str)) { document.write(arr[i] + " " ); } } } // Driver code let str = "ABCD" ; let arr = new Array( "AABCDA" , "ABCDZADC" , "ABCDBCA" , "ABCDABDCA" ); getStrings(str, arr); // This code is contributed by _saurabh_jaiswal </script> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!