• Post Reply Bookmark Topic Watch Topic
  • New Topic

Count the number of occurrences of all characters in a string and print  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have been given the following String - "aabcccde"
Now I want to return a string from a method that would look like this - "a2b1c3d1e1"

I have used HashMap with this and counted the number of occurrences of all characters in the string but cannot form the resulted string.
Here is the code-



P.S. Is there any other I can achieve what I want, I mean without using any Data Structure like a HashMap?
 
Bartender
Posts: 2124
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You can iterate over map.entrySet() and append key+value to a String (possibly StringBuilder).

Do you care about the order of characters in the output?
If you do, would it be the order of first appearance or alphabetical order? Or any other?

For xxxaddb, do you want to get x3a1d2b1 or a1b1d2x3?
 
Aman Grover Grover
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Welcome to the Ranch!

You can iterate over map.entrySet() and append key+value to a String (possibly StringBuilder).

Do you care about the order of characters in the output?
If you do, would it be the order of first appearance or alphabetical order? Or any other?

For xxxaddb, do you want to get x3a1d2b1 or a1b1d2x3?



Yes, the order of the characters is important. Actually this question was asked to me during an interview. The interviewer also said the same can be achieved without using any Data Structure, so is it possible? If yes then how?
 
Bartender
Posts: 3638
47
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep track of the last char seen and a count of how many times the char has been seen. Loop through the string and if the char is the same as the previous char then increment the counter. If not, then print the previous char and its count and set the previous char to the current char and the counter to 1.
 
Rancher
Posts: 1002
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the supposed output for aabaacaa?
 
Aman Grover Grover
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:What is the supposed output for aabaacaa?



Actually the string can only contain consecutive repeating characters. So 'a' cannot appear after 'b'.
 
Aman Grover Grover
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Keep track of the last char seen and a count of how many times the char has been seen. Loop through the string and if the char is the same as the previous char then increment the counter. If not, then print the previous char and its count and set the previous char to the current char and the counter to 1.



Thanks a lot, I have solved it.
 
Ranch Hand
Posts: 78
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add the following code to iterate though the map to get the word and the number of occurrences.

 
Carey Brown
Bartender
Posts: 3638
47
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aman Grover Grover wrote:Thanks a lot, I have solved it.


You're welcome.

Please post your solution as it may be a help to others.
 
Master Rancher
Posts: 2223
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aman Grover Grover wrote:Suppose I have been given the following String - "aabcccde"
Now I want to return a string from a method that would look like this - "a2b1c3d1e1"

I have used HashMap with this and counted the number of occurrences of all characters in the string but cannot form the resulted string.
Here is the code-



P.S. Is there any other I can achieve what I want, I mean without using any Data Structure like a HashMap?


A Map is  ideal for a situation like this. If you want the characters alphabetically, use a TreeMap. You could use a java 8 method 'merge' for this, shortening the code. For instance
 
Sheriff
Posts: 21204
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use a stream to do it all. Note that Collectors.counting() causes the value type to be Long, not Integer.
 
Rancher
Posts: 3255
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the posts implying that the string in question is already grouped, and that the interviewer said it could be done without "data structures", I think Carey's solution sounds about right (track the current character, count them, display it and its count).
 
Dave Tolls
Rancher
Posts: 3255
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking about it, with a view to this being an interview question, I would be tempted to suggest (and have done before now):



If you're presented with a single data point (as we are from the OP) and an expected output, with no rules, then the above fulfills the (limited) requirements.

So long as you remember to point out to the interviewer that this is the case, and you would need more detail to produce something useful.
You might also want to gauge the interviewer's sense of humour beforehand.
;)
 
Marshal
Posts: 5247
364
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aman Grover Grover wrote:

Carey Brown wrote:Keep track of the last char seen and a count of how many times the char has been seen. Loop through the string and if the char is the same as the previous char then increment the counter. If not, then print the previous char and its count and set the previous char to the current char and the counter to 1.



Thanks a lot, I have solved it.


You didn't. Carey did. You just wrote down the rules in Java.

What is very important in programming is being able to solve logical problems. I'd spend at least 72 hours trying before I'd start showing some signs of possibly needed hint.
Since this exercise doesn't require any advanced knowledge, you had to spend much more time on it and solve it yourself. There is no other way to learn.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!