I am not able to get the correct occurrence of each letter present in the string. What is wrong with my code?
Code:
public class consecutiveCharacters {
public static String solve(String A, int B) {
HashMap<String, Integer> map = new HashMap<>();
for (int i=0;i<A.length();i ) {
Integer value = map.get(A.charAt(i));
if(value == null){
map.put(String.valueOf(A.charAt(i)),1);
}
else {
map.put(String.valueOf(A.charAt(i)),i 1);
}
}
System.out.println(map);
String text = "i feel good";
return text;
}
public static void main(String[] args) {
solve("aabbccd",2);
}
}
The output of the map should be:
{a=2, b=2, c=2, d=1}
Yet the output that the above code is giving is:
{a=1, b=1, c=1, d=1}
CodePudding user response:
Using Map.merge()
you can rid of checking this yourself:
Map<Character, Integer> map = new HashMap<>();
for( char c : A.toCharArray()) {
map.merge(c, 1, Integer::sum);
}
merge()
takes 3 parameters:
- A key which is the character in your case
- A value to put for the key, which is 1 in your case
- A merge bi-function to combine the new and existing values if the entry already existed.
Integer::sum
refers toInteger's
methodstatic int sum(int a, int b)
which basically is the same as yourvalue 1
.
CodePudding user response:
I would use the following logic:
public static void solve(String A) {
Map<Character, Integer> map = new HashMap<>();
for (int i=0; i < A.length(); i ) {
char letter = A.charAt(i);
int count = map.get(letter) != null ? map.get(letter) : 0;
map.put(letter, count);
}
System.out.println(map); // prints {a=2, b=2, c=2, d=1}
}
public static void main(String[] args) {
solve("aabbccd");
}
The main changes I made were to switch to using a Map<Character, Integer>
, since we want to keep track of character key counts, rather than strings. In addition, I tightened the lookup logic to set the initial count at either zero, in case a new letter comes along, or the previous value in the map, in case the letter be already known.
CodePudding user response:
A more generic solution is to use Map<Character, Integer>
:
public static void solve(String str) {
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < str.length(); i ) {
char ch = Character.toLowerCase(str.charAt(i));
map.merge(ch, 1, Integer::sum);
}
System.out.println(map);
}
In case you have only English letter, then solution could be more memory effective:
public static void solve(String str) {
str = str.toLowerCase();
int[] letters = new int[26];
for (int i = 0; i < str.length(); i )
letters[str.charAt(i) - 'a'] ;
for (int i = 0; i < letters.length; i )
if (letters[i] > 0)
System.out.format("%c=%d\n", 'a' i, letters[i]);
}
CodePudding user response:
The problem seems to be in this line map.put(String.valueOf(A.charAt(i)),i 1)
where it is resetting the count every time it found a recurrence of a character. You need to get the previous count of a character if it recurred. Try changing the line to map.put(String.valueOf(A.charAt(i)),map.get(String.valueOf(A.charAt(i))) 1)
.
Also you can use Character
directly as the key in your HashMap
object, which will make the code much simpler. Hope the following code will be helpful:
public static String solve(String A, int B) {
HashMap<Character, Integer> count = new HashMap<>();
for(Character ch: A.toCharArray()){
if(count.containsKey(ch) == false){
count.put(ch, 0);
}
count.put(ch, count.get(ch) 1);
}
System.out.println(count);
String text = "i feel good";
return text;
}
// A = "aabbccd"
// count = {a=2, b=2, c=2, d=1}
CodePudding user response:
The reason why it is not working is that you are putting a key of type String and you are trying to get using a character value. The fastest way to solve the bug would be to change the get to
map.get(String.valueOf(A.charAt(i)))
Another way to do a check if a key does not exist is to use the contains method.
map.containsKey(String.valueOf(A.charAt(i)))