Pergunta de entrevista da empresa Microsoft

Implement a simple compression algorithm where repeated letters in a string are represented by a count and the letter. Example: AAACBBD = 3A1C2B1D

Respostas da entrevista

Sigiloso

3 de fev. de 2013

Here's a simple C++ implementation: string compress(const string& str) { if (str.empty()) return str; string newStr; int count(1); char curr(str[0]); char buff[5]; for (int i = 1; i < str.size(); ++i) { // same char -- increment the counter if (curr == str[i]) ++count; else { // append count and the char itoa(count, buff, 10); newStr += string(buff) + curr; // reset variables curr = str[i]; count = 1; } } // append the last group of char(s) itoa(count, buff, 10); newStr += string(buff) + curr; return newStr; }

1

Sigiloso

10 de fev. de 2013

void countChar(char *ar, int size){ int count = 1; for(int i = 0; i < size-1; i++){ if(ar[i] == ar[i+1]){ count++; } else{ cout << count << ar[i]; count = 1; } } }

1

Sigiloso

30 de out. de 2014

public StringBuilder compression(String str) { int count=1; StringBuilder sb = new StringBuilder(); for(int i=0;i