Common characters in n strings
Given n strings, find the common characters in all the strings. In simple words, find characters that appear in all the strings and display them in alphabetical order or lexicographical order.
Note* we’ll be considering that the strings contain lower case letters only.
Examples:
Input : geeksforgeeks
gemkstones
acknowledges
aguelikes
Output : e g k s
Input : apple
orange
Output : a e
We’ll use two hash arrays of size 26 (for a-z, where 0 is a, and z is 25).
The approach will be simple, if we have seen a character before we’ll mark it and if we haven’t then ignore the character because it is not a common one.
Pseudocode :
commonCharacters :
for i= 0 to n-1:
// here m is length of ith string
for j = 0 to m-1:
if ( character seen before ) :
mark the character
else :
ignore it
display all the marked characters
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void commonCharacters(string str[], int n)
{
bool prim[MAX_CHAR];
memset (prim, true , sizeof (prim));
for ( int i = 0; i < n; i++) {
bool sec[MAX_CHAR] = { false };
for ( int j = 0; str[i][j]; j++) {
if (prim[str[i][j] - 'a' ])
sec[str[i][j] - 'a' ] = true ;
}
memcpy (prim, sec, MAX_CHAR);
}
for ( int i = 0; i < 26; i++)
if (prim[i])
printf ( "%c " , i + 'a' );
}
int main()
{
string str[] = { "geeksforgeeks" ,
"gemkstones" ,
"acknowledges" ,
"aguelikes" };
int n = sizeof (str)/ sizeof (str[0]);
commonCharacters(str, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int MAX_CHAR = 26 ;
public static void commonCharacters(String str[],
int n)
{
Boolean[] prim = new Boolean[MAX_CHAR];
Arrays.fill(prim, new Boolean( true ));
for ( int i = 0 ; i < n; i++) {
Boolean[] sec = new Boolean[MAX_CHAR];
Arrays.fill(sec, new Boolean( false ));
for ( int j = 0 ; j < str[i].length(); j++)
{
if (prim[str[i].charAt(j) - 'a' ])
sec[str[i].charAt(j) - 'a' ] = true ;
}
System.arraycopy(sec, 0 , prim, 0 , MAX_CHAR);
}
for ( int i = 0 ; i < 26 ; i++)
if (prim[i]){
System.out.print(Character.toChars(i
+ 97 ));
System.out.print( " " );
}
}
public static void main(String[] args)
{
String str[] = { "geeksforgeeks" ,
"gemkstones" ,
"acknowledges" ,
"aguelikes" };
int n = str.length;
commonCharacters(str, n);
}
}
|
Python3
MAX_CHAR = 26
def commonCharacters(strings, n) :
prim = [ True ] * MAX_CHAR
for i in range (n):
sec = [ False ] * MAX_CHAR
for j in range ( len (strings[i])):
if (prim[ ord (strings[i][j]) - ord ( 'a' )]) :
sec[ ord (strings[i][j]) -
ord ( 'a' )] = True
for i in range (MAX_CHAR):
prim[i] = sec[i]
for i in range ( 26 ):
if (prim[i]) :
print ( "%c " % (i + ord ( 'a' )),
end = "")
strings = [ "geeksforgeeks" , "gemkstones" ,
"acknowledges" , "aguelikes" ]
n = len (strings)
commonCharacters(strings, n)
|
C#
using System;
class GFG
{
static int MAX_CHAR = 26;
public static void commonCharacters(String []str,
int n)
{
Boolean[] prim = new Boolean[MAX_CHAR];
for ( int i = 0; i < prim.Length; i++)
prim[i] = true ;
for ( int i = 0; i < n; i++)
{
Boolean[] sec = new Boolean[MAX_CHAR];
for ( int s = 0; s < sec.Length; s++)
sec[s]= false ;
for ( int j = 0; j < str[i].Length; j++)
{
if (prim[str[i][j] - 'a' ])
sec[str[i][j] - 'a' ] = true ;
}
Array.Copy(sec, 0, prim, 0, MAX_CHAR);
}
for ( int i = 0; i < 26; i++)
if (prim[i])
{
Console.Write(( char )(i + 97));
Console.Write( " " );
}
}
public static void Main(String[] args)
{
String []str = { "geeksforgeeks" ,
"gemkstones" ,
"acknowledges" ,
"aguelikes" };
int n = str.Length;
commonCharacters(str, n);
}
}
|
Javascript
<script>
const MAX_CHAR = 26
function commonCharacters(strings, n) {
let prim = new Array(MAX_CHAR).fill( true )
for (let i = 0; i < n; i++){
let sec = new Array(MAX_CHAR).fill( false )
for (let j = 0; j < strings[i].length; j++){
if (prim[strings[i].charCodeAt(j) - ( 'a' ).charCodeAt(0)])
sec[strings[i].charCodeAt(j) - ( 'a' ).charCodeAt(0)] = true
}
for (let i = 0; i < MAX_CHAR; i++){
prim[i] = sec[i]
}
}
for (let i = 0; i < 26; i++){
if (prim[i])
document.write(String.fromCharCode(i +( 'a' ).charCodeAt(0)), " " )
}
}
let strings = [ "geeksforgeeks" , "gemkstones" ,
"acknowledges" , "aguelikes" ]
let n = strings.length
commonCharacters(strings, n)
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...