Pergunta de entrevista da empresa NVIDIA

Compress string such as 'AAABBCCCCCCAAAAA' to '3A2B6C5A'

Respostas da entrevista

Sigiloso

5 de ago. de 2015

public class Compressor { public static void main(String[] args){ String orig = args[0]; if (args[0] == null) return; char current = orig.charAt(0); int count = 1; String result = new String(); for (int i=1; i < orig.length(); i++){ System.out.println(i +"\t" + current +"\t" + count); if (orig.charAt(i)== current){ count ++; } else { result += Integer.toString(count) + current; current = orig.charAt(i); count = 1; } } result += Integer.toString(count) + current; System.out.println(result); } }

2

Sigiloso

21 de mar. de 2017

def compress(ipStr): ans = "" cnt = 1 for i in range(len(ipStr)-1): if ipStr[i] == ipStr[i+1]: cnt = cnt+1 else: ans = ans + str(cnt) + ipStr[i] cnt = 1 ans = ans + str(cnt) + ipStr[i] return ans