Write program to print Hello World and print each character
address in that string and print how many times each
character is in that string?
Ex: H: 0 & 1
e:1 & 1
l :2,3,8 & 3
o:4,6 & 2
w:5 & 1
r: 7 & 1
d 9 & 1
Answer Posted / jishnu
Sorry I dont want to give a detailed answer here cos no one e is going to read the code.
You can use Map<Char,List<Integer>> counterMap
Iterate through the length of the string
for(int i=i; i<s.length();i++){
if(counterMap.get(s.getCharAt(i))==nul){
//First time
List<Integer> a= new ArrayList<Integer>();
a.add(i);
counterMap.put(s.getCharAt(i),a);
}else{
counterMap.get(s.getCharAt(i)).add(i);
}
//We can iterate through keySet or entrySet to show the result
}
Regards,
Jishnu
Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
What is the purpose of the strictfp keyword?
Is java type safe?
Can constructor return value?
How list contains works in java?
What does java stand for?
What is the size of an array?
What is a singleton class in Java?
Is there any sort function in java?
What are the restriction imposed on a static method or a static block of code?
Difference between this() and super() in java ?
I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor’s body?
What is the structure of java?
What are the steps that are followed when two computers connect through tcp?
Do you need to import math in java?
What is unicode datatype?