Pergunta de entrevista da empresa Microsoft

string compression: aaabbbbcc ->a3b4c2

Respostas da entrevista

Sigiloso

11 de jul. de 2012

Java code: private static String compressString(String input) { StringBuilder result = new StringBuilder(); char currentCharacter; int count; for(int i = 0; i < input.length(); i++) { currentCharacter = input.charAt(i); count = 1; while(i < input.length() - 1 && input.charAt(i + 1) == currentCharacter) { count++; i++; } result.append(currentCharacter); result.append(count); } return result.toString(); }

3

Sigiloso

4 de out. de 2012

//C++ using namepsace std; main(int argc, char** argv) { string input = "aaaabbccccddd"; string output; char lastchar = input[0]; int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] != lastchar || i == input.size() -1 ) { output += lastchar; output += count; count = 0; lastchar = input[i]; } count++; } }

Sigiloso

29 de out. de 2012

What is a character is repeated more than 10 times? aaa..14times..bbb.cc.. a14b3c3 - how do we decode it then? if we assume input string can contain only alphabets- then its fine.

Sigiloso

18 de nov. de 2013

Make an array of size 256 for all ASCII characters. Iterate through string and increment each character that matches the array. Iterate through ASCII array and concatenate letters and element values

Sigiloso

27 de out. de 2014

I'd probably just use a hash map (in-order map just like C++'s map) and iterate through the string counting the number of occurrences of each character. Then, using the in-order property of the hash table, the string could then be printed out in the aforementioned format: aaabbbbcc ->a3b4c2.