Type-1:
Let's implement a Java method that takes two strings as input and returns the common characters between them.
We'll use a Set data structure to efficiently find the common characters. Here's the implementation:
import java.util.HashSet;
import java.util.Set;
public class CommonCharacterFinder {
public static void main(String[] args) {
String input1 = "hello";
String input2 = "world";
Set<Character> commonChars = findCommonCharacters(input1, input2);
System.out.println("Common characters: " + commonChars);
}
public static Set<Character> findCommonCharacters(String str1, String str2) {
// Convert the input strings to lowercase (if case-insensitive matching is desired)
String lowercaseStr1 = str1.toLowerCase();
String lowercaseStr2 = str2.toLowerCase();
// Create a set to store characters in the first string
Set<Character> set1 = new HashSet<>();
for (char ch : lowercaseStr1.toCharArray()) {
set1.add(ch);
}
// Create a set to store common characters between both strings
Set<Character> commonChars = new HashSet<>();
// Iterate through the characters of the second string
for (char ch : lowercaseStr2.toCharArray()) {
// If the character is present in both sets, it is a common character
if (set1.contains(ch)) {
commonChars.add(ch);
}
}
return commonChars;
}
}
Explanation:
- The code structure and the declaration of the main method are provided as usual. In the main method, we declare and initialize two input strings input1 and input2.
- We call the findCommonCharacters method, passing input1 and input2 as arguments, to find the common characters between the two strings.
- In the findCommonCharacters method, we convert both input strings to lowercase using the toLowerCase() method. This step ensures case-insensitive matching if required.
- We create a HashSet called set1 to store characters from the first string str1. We iterate through the characters of lowercaseStr1 using a for-each loop and add each character to set1. The use of a HashSet allows us to quickly check if a character exists in set1 in constant time.
- We create another HashSet called commonChars to store the common characters between both strings.
- Next, we iterate through the characters of lowercaseStr2 using a for-each loop.
- For each character ch, we check if it exists in set1 using the contains() method.
- If ch is present in both set1 and lowercaseStr2, it means the character is common between the two strings, so we add it to the commonChars set.
- After processing all characters from lowercaseStr2, the commonChars set contains the common characters between str1 and str2.
- Finally, we return the commonChars set as the result of the method.
By using this approach with two HashSet data structures, we efficiently find the common characters between two input strings while handling case-insensitive matching if required. The method performs well for both short and long strings, as HashSet provides efficient lookups and ensures that duplicates are not stored in the set.
Type-2:
Another way to find the common characters between two strings is by using an array or a HashMap to track the frequency of characters in one string and then comparing the characters in the second string with the tracked frequencies.
Here's the implementation using a HashMap:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CommonCharacterFinder {
public static void main(String[] args) {
String input1 = "hello";
String input2 = "world";
Set<Character> commonChars = findCommonCharacters(input1, input2);
System.out.println("Common characters: " + commonChars);
}
public static Set<Character> findCommonCharacters(String str1, String str2) {
// Convert the input strings to lowercase (if case-insensitive matching is desired)
String lowercaseStr1 = str1.toLowerCase();
String lowercaseStr2 = str2.toLowerCase();
// Create a HashMap to store the frequency of characters in str1
Map<Character, Integer> charFrequencyMap = new HashMap<>();
for (char ch : lowercaseStr1.toCharArray()) {
charFrequencyMap.put(ch, charFrequencyMap.getOrDefault(ch, 0) + 1);
}
// Create a set to store common characters between both strings
Set<Character> commonChars = new HashSet<>();
// Iterate through the characters of str2
for (char ch : lowercaseStr2.toCharArray()) {
// If the character exists in the frequency map and its count is greater than 0, it is a common character
int frequency = charFrequencyMap.getOrDefault(ch, 0);
if (frequency > 0) {
commonChars.add(ch);
charFrequencyMap.put(ch, frequency - 1);
// Decrease the count to avoid duplicates
}
}
return commonChars;
}
}
Explanation:
- The code structure and the declaration of the main method are provided as before.
- We call the findCommonCharacters method, passing input1 and input2 as arguments, to find the common characters between the two strings.
- In the findCommonCharacters method, we convert both input strings to lowercase using the toLowerCase() method. This step ensures case-insensitive matching if required.
- We create a HashMap called charFrequencyMap to store the frequency of characters in str1. We iterate through the characters of lowercaseStr1 using a for-each loop and populate the charFrequencyMap with the frequency of each character. The frequency is tracked by incrementing the count in the map for each occurrence of a character.
- We create a HashSet called commonChars to store the common characters between both strings.
- Next, we iterate through the characters of lowercaseStr2 using a for-each loop.
- For each character ch, we check if it exists in the charFrequencyMap and retrieve its frequency using the getOrDefault() method. If the character is present and its frequency is greater than 0, it means the character is common between the two strings.
- We add the common character to the commonChars set, and to avoid duplicates, we decrease its frequency in the charFrequencyMap.
- After processing all characters from lowercaseStr2, the commonChars set contains the common characters between str1 and str2.
- Finally, we return the commonChars set as the result of the method.
By using this approach with a HashMap to track the frequency of characters, we efficiently find the common characters between two input strings while handling case-insensitive matching if required. The method performs well for both short and long strings, and it avoids using nested loops, which could have a higher time complexity.
0 Comments