Pergunta de entrevista da empresa Amazon

write a function to print the frequency of characters eg. Input : aabbbc Output: a2b3c1

Respostas da entrevista

Sigiloso

31 de jan. de 2011

Complexity is O(n) class CharsFrequency { public static Hashtable CharacterFrequency(string strInput) { Hashtable ht = new Hashtable(); foreach (char item in strInput) { if (ht.ContainsKey(item)) { ht[item] = Int32.Parse(ht[item].ToString()) + 1; } else { ht[item] = 1; } } return ht; } }

1

Sigiloso

4 de fev. de 2011

Easier method without hashtable int count = 0; for (int i = 0; i < string.length();i ++) { count++; if (i < string.length()-1) { if (simple.charAt(i) != string.charAt(i+1)) { System.out.print(string.charAt(i) + "" + count); count = 0; } } else if (i==string.length()-1) { //last character System.out.print(string.charAt(i) + "" + count); } }

1