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 the purpose of the strictfp keyword?

618


Is java type safe?

515


Can constructor return value?

516


How list contains works in java?

563


What does java stand for?

573






What is the size of an array?

557


What is a singleton class in Java?

520


Is there any sort function in java?

588


What are the restriction imposed on a static method or a static block of code?

603


Difference between this() and super() in java ?

624


I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor’s body?

595


What is the structure of java?

515


What are the steps that are followed when two computers connect through tcp?

530


Do you need to import math in java?

569


What is unicode datatype?

536