Pergunta de entrevista da empresa Capital One

Given a string, return the first NON-repeating character that occurs in the string. EX: "adzbdcab" returns 'z'.

Respostas da entrevista

Sigiloso

5 de set. de 2017

Okay, idk WTF wrong with y'all. This "lastIndexOf" has a runtime of O(m*n) based on the KMP algorithm. You CANNOT use it! You're essentially having an O(n^2) solution! Here's a solution WITH hashing (faster than a hash table too!) def non_repeating(string): if(string is None): return None hash_table = [0] * 256 for char in string: hash_table[ord(char)] += 1 for char in string: if(hash_table[ord(char)] == 1): return char return None

4

Sigiloso

25 de mar. de 2019

How about this: ################### raw_string = "adzbdcab" result = [] dup = [] for i in range(len(raw_string)): if raw_string[i] in result: result.remove(raw_string[i]) dup.append(raw_string[i]) else: if raw_string[i] not in dup: result.append(raw_string[i]) print(result[0])

Sigiloso

29 de ago. de 2019

import java.util.*; public class MyClass { public static void main(String args[]) { String s="abdzdbca"; LinkedHashMap h= new LinkedHashMap(); for(char a: s.toCharArray()){ h.put(a, h.get(a)==null? 1 : h.get(a).intValue()+1 ); if(h.get(a)==2){ h.remove(a); } } System.out.println("h=" + new ArrayList(h.keySet()).get(0)); } }

Sigiloso

22 de abr. de 2015

String s = "adzbdcab"; char[] chars = s.toCharArray(); for(char c: chars){ if(s.indexOf(c)==s.lastIndexOf(c)){ System.out.println(" character is "+ c ); break; } }

4

Sigiloso

1 de dez. de 2015

function nonRepeater (input) { var charCount = {}; for (var i=0; i

2

Sigiloso

14 de dez. de 2016

def string(string) arr = string.split('') arr.count.times do str = arr.shift return str unless arr.include? str end end

Sigiloso

6 de mar. de 2015

Using a O(n) solution, not a nested for loops solution that's O(n^2).

Sigiloso

24 de abr. de 2015

faster one.. int length = s.length(); char c; for(int i=0; i

1