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 an infinite loop in java? Explain with an example.
What is the most important feature of java? What is an interface?
How do you escape a string?
Explain about serializable interface in java?
What is a ternary operator in java? What is an interface?
Where local and global variables are stored?
What is the size of integer?
What do you understand by abstract classes?
Can you give few examples of final classes defined in java api?
Can we have any code between try and catch blocks?
Is namespace same as package in java?
What is static method with example?
What does those terms actually mean included in the j.d.k i.6?
What is the synonym of string?
What happens if I remove static from main method?