Java String manipulation based on most frequent char count -


in recent interview, asked find solution below string manipulation program. given string s, represent frequent character 1, 2nd frequent character 01, third 001 etc.

if string "marrymyyyr", output should :

char count each character m:2 , a:1, r:3, y:4 highest count number 4 character count should printed 1 inplace of char, char count 3 should printed 01 inplace of char , on.

output : 001(m)0001(a)01(r)01(r)1(y)001(m)1(y)1(y)1(y)01(r)

i used hashmap keep track of count each character. unable solve problem. know have implement mechanism apply hashing/mapping hashmap based on frequent char mapped correspondence resultant string combination of"1" "0" , print char mapped resultant string instead of char.

you said used hashmap. if created map map<character, atomicinteger>, can update value directly, without affecting map.

this improves performance of counting step of process, allows replace "count" value value representing number of 0's print character, aka "rank".

example: counts being m:2, a:1, r:3, y:4 (from question), sort list descending (y:4, r:3, m:2, a:1), replace counts incrementing ranks: y:0, r:1, m:2, a:3.

if use linkedhashmap, multiple characters equal count ranked in order of first appearance.

with ranks, can convert input. (the following java version. on java 8, lambdas reduce code lot. i'll leave exercise you.)

private static string test(string input) {     char[] chars = input.tochararray();      // collect chars count of occurrences     map<character, atomicinteger> charmap = new linkedhashmap<>();     (character c : chars) {         atomicinteger count = charmap.get(c);         if (count == null)             charmap.put(c, new atomicinteger(1));         else             count.incrementandget();     }      // sort char/count pairs count (descending)     @suppresswarnings("unchecked")     entry<character, atomicinteger>[] chararr = charmap.entryset().toarray(new map.entry[charmap.size()]);     arrays.sort(chararr, new comparator<entry<character, atomicinteger>>() {         @override         public int compare(entry<character, atomicinteger> e1, entry<character, atomicinteger> e2) {             return integer.compare(e2.getvalue().intvalue(), e1.getvalue().intvalue()); // descending         }     });      // replace "count" "rank" (this updates values in charmap)     (int = 0; < chararr.length; i++)         chararr[i].getvalue().set(i);      // generate result     stringbuilder buf = new stringbuilder();     (character c : chars) {         int rank = charmap.get(c).intvalue();         while (rank-- > 0)             buf.append('0');         buf.append('1');         buf.append('(').append(c).append(')'); // remove?     }     return buf.tostring(); } 

test

system.out.println(test("marrymyyyr")); 

output

001(m)0001(a)01(r)01(r)1(y)001(m)1(y)1(y)1(y)01(r) 

the phrasing of question makes sound letters should replaced ("inplace of"). if so, comment out or delete line marked // remove?

output

00100010101100111101 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -