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


Please Help Members By Posting Answers For Below Questions

Explain the difference between protected and default access.

513


What is the difference between class forname and new?

552


Explain the importance of thread scheduler in java?

600


What is anagram number?

456


Difference between operator overloading and function overloading

613






What is the difference between a static and a non-static inner class in java programming?

533


Why do we need variables?

531


How do you know if a value is nan?

568


What is maximum size of arraylist in java?

495


Can we overload the methods by making them static?

505


Is null or empty java?

533


how to create constants in java?

606


What defines function?

542


What are access specifiers in java ?

590


What do you understand by Header linked List?

592