Given a string array **arr[]**, the task is to find the longest sub-sequence of the array with at least one character appearing in all the strings. **Note** that all the strings contain only lowercase English alphabets.

**Examples:**

Input:str = {“ab”, “bc”, “de”}

Output:2

{“ab”, “bc”} is the required sub-sequence

with ‘b’ as the common character.

Input:str = {“a”, “b”, “c”}

Output:1

**Approach:** Create a **count[]** array such that **count[0]** will store the number of strings which contain **‘a’**, **count[1]** will store the number of strings which contain **‘b’** and so on…

Now, it’s clear that the answer will be the maximum value from the **count[]** array. In order to update this array start traversing the string array and for every string, mark which characters are present in the current string in a **hash[]** array.

And after the traversal, for every character which is present in the current string update its count in the **count[]** array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 26 ` ` ` `// Function to return the length of the longest ` `// sub-sequence with at least one ` `// common character in every string ` `int` `largestSubSeq(string arr[], ` `int` `n) ` `{ ` ` ` ` ` `// count[0] will store the number of strings ` ` ` `// which contain 'a', count[1] will store the ` ` ` `// number of strings which contain 'b' and so on.. ` ` ` `int` `count[MAX] = { 0 }; ` ` ` ` ` `// For every string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `string str = arr[i]; ` ` ` ` ` `// Hash array to set which character is ` ` ` `// present in the current string ` ` ` `bool` `hash[MAX] = { 0 }; ` ` ` `for` `(` `int` `j = 0; j < str.length(); j++) { ` ` ` `hash[str[j] - ` `'a'` `] = ` `true` `; ` ` ` `} ` ` ` ` ` `for` `(` `int` `j = 0; j < MAX; j++) { ` ` ` ` ` `// If current character appears in the ` ` ` `// string then update its count ` ` ` `if` `(hash[j]) ` ` ` `count[j]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `*(max_element(count, count + MAX)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"ab"` `, ` `"bc"` `, ` `"de"` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(string); ` ` ` ` ` `cout << largestSubSeq(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `MAX = ` `26` `; ` ` ` ` ` `// Function to return the length of the longest ` ` ` `// sub-sequence with at least one ` ` ` `// common character in every string ` ` ` `static` `int` `largestSubSeq(String arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// count[0] will store the number of strings ` ` ` `// which contain 'a', count[1] will store the ` ` ` `// number of strings which contain 'b' and so on.. ` ` ` `int` `[] count = ` `new` `int` `[MAX]; ` ` ` ` ` `// For every string ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `String str = arr[i]; ` ` ` ` ` `// Hash array to set which character is ` ` ` `// present in the current string ` ` ` `boolean` `[] hash = ` `new` `boolean` `[MAX]; ` ` ` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < str.length(); j++) { ` ` ` `hash[str.charAt(j) - ` `'a'` `] = ` `true` `; ` ` ` `} ` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < MAX; j++) { ` ` ` ` ` `// If current character appears in the ` ` ` `// string then update its count ` ` ` `if` `(hash[j]) ` ` ` `count[j]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `max = -` `1` `; ` ` ` ` ` `for` `(` `int` `i=` `0` `;i< MAX; i++) ` ` ` `{ ` ` ` `if` `(max < count[i]) ` ` ` `max = count[i]; ` ` ` `} ` ` ` `return` `max; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `String arr[] = { ` `"ab"` `, ` `"bc"` `, ` `"de"` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(largestSubSeq(arr, n)); ` ` ` ` ` ` ` `} ` ` ` ` ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `26` ` ` `# Function to return the length of the longest ` `# sub-sequence with at least one ` `# common character in every string ` `def` `largestSubSeq(arr, n): ` ` ` ` ` `# count[0] will store the number of strings ` ` ` `# which contain 'a', count[1] will store the ` ` ` `# number of strings which contain 'b' and so on.. ` ` ` `count ` `=` `[` `0` `] ` `*` `MAX` ` ` ` ` `# For every string ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `string ` `=` `arr[i] ` ` ` ` ` `# Hash array to set which character is ` ` ` `# present in the current string ` ` ` `_hash ` `=` `[` `False` `] ` `*` `MAX` ` ` `for` `j ` `in` `range` `(` `len` `(string)): ` ` ` `_hash[` `ord` `(string[j]) ` `-` `ord` `(` `'a'` `)] ` `=` `True` ` ` ` ` `for` `j ` `in` `range` `(` `MAX` `): ` ` ` ` ` `# If current character appears in the ` ` ` `# string then update its count ` ` ` `if` `_hash[j] ` `=` `=` `True` `: ` ` ` `count[j] ` `+` `=` `1` ` ` ` ` `return` `max` `(count) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[ ` `"ab"` `, ` `"bc"` `, ` `"de"` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(largestSubSeq(arr, n)) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `MAX = 26; ` ` ` ` ` `// Function to return the length of the longest ` ` ` `// sub-sequence with at least one ` ` ` `// common character in every string ` ` ` `static` `int` `largestSubSeq(` `string` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// count[0] will store the number of strings ` ` ` `// which contain 'a', count[1] will store the ` ` ` `// number of strings which contain 'b' and so on.. ` ` ` `int` `[] count = ` `new` `int` `[MAX]; ` ` ` ` ` `// For every string ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `string` `str = arr[i]; ` ` ` ` ` `// Hash array to set which character is ` ` ` `// present in the current string ` ` ` `bool` `[] hash = ` `new` `bool` `[MAX]; ` ` ` ` ` ` ` `for` `(` `int` `j = 0; j < str.Length; j++) ` ` ` `{ ` ` ` `hash[str[j] - ` `'a'` `] = ` `true` `; ` ` ` `} ` ` ` ` ` `for` `(` `int` `j = 0; j < MAX; j++) ` ` ` `{ ` ` ` ` ` `// If current character appears in the ` ` ` `// string then update its count ` ` ` `if` `(hash[j]) ` ` ` `count[j]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `max = -1; ` ` ` ` ` `for` `(` `int` `i=0;i< MAX; i++) ` ` ` `{ ` ` ` `if` `(max < count[i]) ` ` ` `max = count[i]; ` ` ` `} ` ` ` `return` `max; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` ` ` `string` `[] arr = { ` `"ab"` `, ` `"bc"` `, ` `"de"` `}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(largestSubSeq(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Longest subsequence where every character appears at-least k times
- Longest Subsequence with at least one common digit in every element
- Lexicographically largest subsequence such that every character occurs at least k times
- Map every character of one string to another such that all occurrences are mapped to the same character
- Longest subsequence where each character occurs at least k times
- Longest subsequence such that adjacent elements have at least one common digit
- Sub-strings that start and end with one character and have at least one other
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find length of longest subsequence of one string which is substring of another string
- Check if both halves of the string have at least one different character
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimize length of Substrings containing at least one common Character
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count of N size strings consisting of at least one vowel and one consonant
- Longest string which is prefix string of at least two strings

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.