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 differences between abstraction and encapsulation?

512


What is a website container?

496


What is size_t?

499


Why should I use abstract class?

560


What is equlas() and hashcode() contract in java? Where does it used?

600






Why is an interface be able to extend more than one interface but a class can’t extend more than one class?

697


What does flagged out mean?

563


What is passed by reference and pass by value ?

576


What is a lambda expression ? What's its use ?

558


Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?

583


What is meant by attribute?

588


What is the difference between form & report?

584


Can you run java program without main method?

541


what is static import in java? Explain

547


What do you mean by jjs in java8?

552