Print common characters of two Strings in alphabetical order
Last Updated :
11 Jul, 2022
Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case.
Examples:
Input :
string1 : geeks
string2 : forgeeks
Output : eegks
Explanation: The letters that are common between
the two strings are e(2 times), k(1 time) and
s(1 time).
Hence the lexicographical output is "eegks"
Input :
string1 : hhhhhello
string2 : gfghhmh
Output : hhh
The idea is to use character count arrays.
- Count occurrences of all characters from ‘a’ to ‘z’ in the first and second strings. Store these counts in two arrays a1[] and a2[].
- Traverse a1[] and a2[] (Note size of both is 26). For every index i, print character ‘a’ + i number of times equal min(a1[i], a2[i]).
Below is the implementation of the above steps.
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1 = "geeksforgeeks" ;
string s2 = "practiceforgeeks" ;
int a1[26] = {0};
int a2[26] = {0};
int i , j;
char ch;
char ch1 = 'a' ;
int k = ( int )ch1, m;
for (i = 0 ; i < s1.length() ; i++)
{
a1[( int )s1[i] - k]++;
}
for (i = 0 ; i < s2.length() ; i++)
{
a2[( int )s2[i] - k]++;
}
for (i = 0 ; i < 26 ; i++)
{
if (a1[i] != 0 and a2[i] != 0)
{
for (j = 0 ; j < min(a1[i] , a2[i]) ; j++)
{
m = k + i;
ch = ( char )(k + i);
cout << ch;
}
}
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class Simstrings
{
static final int MAX_CHAR = 26 ;
static void printCommon(String s1, String s2)
{
int [] a1 = new int [MAX_CHAR];
int [] a2 = new int [MAX_CHAR];
int length1 = s1.length();
int length2 = s2.length();
for ( int i = 0 ; i < length1 ; i++)
a1[s1.charAt(i) - 'a' ] += 1 ;
for ( int i = 0 ; i < length2 ; i++)
a2[s2.charAt(i) - 'a' ] += 1 ;
for ( int i = 0 ; i < MAX_CHAR ; i++)
{
if (a1[i] != 0 && a2[i] != 0 )
{
for ( int j = 0 ; j < Math.min(a1[i], a2[i]) ; j++)
System.out.print((( char )(i + 'a' )));
}
}
}
public static void main(String[] args) throws IOException
{
String s1 = "geeksforgeeks" , s2 = "practiceforgeeks" ;
printCommon(s1, s2);
}
}
|
Python3
MAX_CHAR = 26
def printCommon( s1, s2):
a1 = [ 0 for i in range (MAX_CHAR)]
a2 = [ 0 for i in range (MAX_CHAR)]
length1 = len (s1)
length2 = len (s2)
for i in range ( 0 ,length1):
a1[ ord (s1[i]) - ord ( 'a' )] + = 1
for i in range ( 0 ,length2):
a2[ ord (s2[i]) - ord ( 'a' )] + = 1
for i in range ( 0 ,MAX_CHAR):
if (a1[i] ! = 0 and a2[i] ! = 0 ):
for j in range ( 0 , min (a1[i],a2[i])):
ch = chr ( ord ( 'a' ) + i)
print (ch, end = '')
if __name__ = = "__main__" :
s1 = "geeksforgeeks"
s2 = "practiceforgeeks"
printCommon(s1, s2);
|
C#
using System;
public class Simstrings
{
static int MAX_CHAR = 26;
static void printCommon( string s1, string s2)
{
int [] a1 = new int [MAX_CHAR];
int [] a2 = new int [MAX_CHAR];
int length1 = s1.Length;
int length2 = s2.Length;
for ( int i = 0 ; i < length1 ; i++)
a1[s1[i] - 'a' ] += 1;
for ( int i = 0 ; i < length2 ; i++)
a2[s2[i] - 'a' ] += 1;
for ( int i = 0 ; i < MAX_CHAR ; i++)
{
if (a1[i] != 0 && a2[i] != 0)
{
for ( int j = 0 ; j < Math.Min(a1[i], a2[i]) ; j++)
Console.Write((( char )(i + 'a' )));
}
}
}
public static void Main()
{
string s1 = "geeksforgeeks" , s2 = "practiceforgeeks" ;
printCommon(s1, s2);
}
}
|
Javascript
<script>
let MAX_CHAR = 26;
function printCommon(s1,s2)
{
let a1 = new Array(MAX_CHAR);
let a2 = new Array(MAX_CHAR);
for (let i=0;i<MAX_CHAR;i++)
{
a1[i]=0;
a2[i]=0;
}
let length1 = s1.length;
let length2 = s2.length;
for (let i = 0 ; i < length1 ; i++)
a1[s1[i].charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
for (let i = 0 ; i < length2 ; i++)
a2[s2[i].charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
for (let i = 0 ; i < MAX_CHAR ; i++)
{
if (a1[i] != 0 && a2[i] != 0)
{
for (let j = 0 ; j < Math.min(a1[i], a2[i]) ; j++)
document.write((String.fromCharCode(i + 'a' .charCodeAt(0))));
}
}
}
let s1 = "geeksforgeeks" , s2 = "practiceforgeeks" ;
printCommon(s1, s2);
</script>
|
Time Complexity: If we consider n = length(larger string), then this algorithm runs in O(n) complexity.
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...