Based on the occurance of the alphabets it should append the appropriate numbers before the alphabets.
So String a ="abcba" should be replaced by "2a2bc".
Please suggest a snippet which will return the result for me.
Thanks in advance
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
1
Assuming we're talking about lowercase ASCII characters only, the solution could involve a "int[] letterCount = new int[26]" field.
For a more generalized problem, a "Map<Character,Integer> letterCount = new HashMap<Character,Integer>" would work.
In either case two loops would be necessary - one to count the letters, and one to create the resulting string.
Siddhesh Deodhar
Ranch Hand
Joined: Mar 05, 2009
Posts: 117
posted
0
You should be taking each character and put it to a map and raise "value" every time you find occurrence of key example
iterative representation of String s will be
<a,1>
<b,1>
<c,1>
<a,2>
<b,2>
<c,1>
and than print this in desired way.
Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )