CC-20 : Find common characters in two Strings.
Description:
Given input Strings str1 and str2, find and return the common characters.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| str1="Listen"; str2="Silo"; |
Common Characters: l,i,s |
| str1="Secure"; str2="ure"; |
Common Characters: e,u,r |
| str1="first"; str2="second"; |
Common Characters: s |
Pseudocode:
| The java method should accept following input parameters: str1 (String), str2 (String). |
| Call String.trim() method on str1 and str2 to remove leading and trailing spaces. |
| Call String.toLowerCase() method on str1 and str2 to make the characters lowercase. |
| Initialize char array chars1=str1.toCharArray(). |
| Initialize char array chars2=str2.toCharArray(). |
| Intialize ArrayList common to store common characters that are found. |
| Iterate through chars1 using for loop using idx1 as loop variable, starting from index 0 and incrementing idx1 till we reach chars1.length-1:
Iterate through chars2 using for loop using idx2 as loop variable, starting from index 0 and incrementing idx2 till we reach chars2.length-1:
If chars1[idx1]==chars2[idx2]:
If common does not contain chars1[idx1], add chars1[idx] to it.
|
| At end of above loop, extract chars list from common into chars array commonChars using a for loop. |
| Now commonChars contains the common characters in str1 and str2, return commonChars. |
The above algorithm can also be completed using Java String APIs:
| Refer code below. The only difference while using String APIs is that we do not extract characters from str1 and str2. We use String.subsquence() method to access characters at various indexes and use String.contains() method to check for common characters. Rest of the algorithm remains the same. |
Code:
public char[] stringFindCommonChars(String str1, String str2) {
str1=str1.trim().toLowerCase();
str2=str2.trim().toLowerCase();
char[] chars1=str1.toCharArray();
char[] chars2=str2.toCharArray();
ArrayList<Character> common=new ArrayList<Character>();
for (int idx1=0; idx1< chars1.length;idx1++) {
for (int idx2=0; idx2< chars2.length;idx2++) {
if (chars1[idx1]==chars2[idx2]) {
if (!common.contains(Character.valueOf(chars1[idx1]))) {
common.add(chars1[idx1]);
break;
}
}
}
}
char[] commonChars=new char[common.size()];
for (int idx=0; idx < common.size(); idx++) {
commonChars[idx]=common.get(idx);
}
return commonChars;
}
public char[] stringFindCommonCharsWithAPI(String str1, String str2) {
str1=str1.trim().toLowerCase();
str2=str2.trim().toLowerCase();
ArrayList<Character> common=new ArrayList<Character>();
for (int idx1=0; idx1< str1.length();idx1++) {
if (str2.contains(str1.subSequence(idx1, idx1+1))) {
if (!common.contains(str1.charAt(idx1))) {
common.add(str1.charAt(idx1));
}
}
}
char[] commonChars=new char[common.size()];
for (int idx=0; idx < common.size(); idx++) {
commonChars[idx]=common.get(idx);
}
return commonChars;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |