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

What is meant by object oriented programming – oop?

639


Give me example of derived data types.

703


Write a java program that prints all the values given at command-line.

652


Can you explain the private protected field modifier?

664


design an lru cache in java?

634






What is __ init __ functions?

652


what is optional in java 8?

675


what is an objects lock and which objects have locks? : Java thread

640


which one is more efficient int x; 1. if(x==null) 2.if(null==x) state which one either 1 or 2?

9645


What is method in research paper?

670


What is stack example?

647


How do you use find and replace?

592


What are the basic interfaces of java collections framework?

703


Can I uninstall java?

646


List two java ide’s?

718