Create HashMap, put to this map all chars from first word with zero value. After that iterate over chars in second word and if this chars contains in map then increase map value for this char.
Map<Character, Integer> map = new HashMap<>();
for(char c: word1.toCharArray()) {
map.put(c, 0);
}
for(char c: word2.toCharArray()) {
Integer count = map.get(c);
if(count != null) {
count++;
map.put(c, count);
}
}
for(Map.Entry<Character, Integer> entry: map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue() + " times");
}
6
solved In java how to compare two strings with a random generate percent result? [closed]