Microsoft Interview Question

Find count of unique characters in a given string

Interview Answers

Anonymous

Jul 27, 2012

Using single iteration :) ..... int map[256]={0}; //initialise map to 0 int count=0; string a="abcabnaabcabaccacbbbqf"; for(int i=0;i

4

Anonymous

Jul 9, 2012

public void counter(String g) { int count; for (int i = 'a' ; i<='z' ; i++){ count = 0; for (int a = 0; a

Anonymous

May 30, 2012

What kind of characters in the string? Assuming ASCII characters, total 256. Array should be a good choose. int uniqueCount(string str){ if(str.size()<2) return str.size(); int charNum[256]={0}; // initialized to zero //counting concurrency for(int i=0; i

Anonymous

Jun 3, 2012

here are two ways of attacking this: (1) sort the chars, then walk the list incrementing a count any time you hit a different char (2) take advantage of the fact that there are at most 256 ASCII characters, with values 0 to 255, so build a 256 sized array to hold all possible chars, then as you walk the list, increment the corresponding array entry holding a matching char. first approach: /* sort the string */ for (int i=0; i