Frage im Vorstellungsgespräch bei Amazon

Given a string find the first non-repeated character.

Antworten zu Vorstellungsgespräch

Anonym

13. Feb. 2011

@Rajiv : Your solution is completely wrong. It will fail for input of "aaa" Reason: on first check, you insert "a". On next check you remove it. On next check you again insert it and return that as your answer, even though it was repeated thrice.

1

Anonym

15. Aug. 2013

program to find first non repeating character in a string c# - three ways

1

Anonym

6. Sept. 2013

My python implementation: def firstNonRepeatingCharacter(inputString): hashmap = {} for x in inputString: if (x in hashmap): hashmap[x] = hashmap[x] + 1 else: hashmap[x] = 1 for x in inputString: if (hashmap[x] > 1): continue else: return x return "No nonrepeating character found"

1

Anonym

23. März 2015

for (int index = 0; index < str.Length; index++) { if (str.LastIndexOf(str[index]) == str.IndexOf(str[index])) { return str[index].ToString(); } }

Anonym

3. Mai 2015

I will have one variable x to store the first non repeating character, variable y as a backup And a set of characters already encountered. I will start from position n to position 0 where n is the lenght of the string. If char(n) is not present in the set, then check if x=char(n), if yes, x = y. If no, y=x and x=char(n). The idea is to keep updating the newest character that has not been repeated and also keeping the second newest character as a backup. In the end return x.

Anonym

12. März 2020

## In Python from collections import Counter def lh(str): lst1 = [] lst2 = [] m = Counter(str) for i, j in m.items(): if j == 1: lst1.append(i) lst2.append(j) print(lst1[0],lst2[0]) lh('aaabbcccdeee') lh('jkshciushgdyiegcigidbircbrot7bRNXBYREICXN')

Anonym

12. März 2020

@Kumar Manish(Your solution sir,) ## My python3 program from collections import Counter def lh(str): lst1 = [] lst2 = [] m = Counter(str) for i, j in m.items(): if j == 1: lst1.append(i) lst2.append(j) if len(lst1) == 0: print('null') else: print(lst1[0], lst2[0]) lh('aaabbcccdeee') lh('jkshciushgdyiegcigidbircbrot7bRNXBYREICXN') lh('aaa')

Anonym

1. Mai 2011

hash of non repeating characters tied to a double linked list, remove any repeating character from the hash and the list. at the end the head of the list is the answer. you can use the same hash to keep the counters.

Anonym

15. Juni 2012

public static String findFirstNonRepeatedCharacter(String S) { int[] T = new int[256]; for (int i = 0; i < S.length(); i++) { char next = S.charAt(i); T[next] = T[next] + 1; } for (int i = 0; i < S.length(); i++) { if(T[S.charAt(i)] == 1) return String.valueOf(S.charAt(i)); } return null; }

Anonym

15. Aug. 2013

Three ways to find first non repeating character in a string c# find the code below - public char firstNonRepetitive(string inputString) { int nextOccurrence = 0; char firstDistinctChar = ' '; for (int i = 0; i < inputString.Length; i++) { nextOccurrence = 0; for (int j = (i + 1); j < inputString.Length; j++) { if (inputString[i] == inputString[j]) nextOccurrence++; } if (nextOccurrence == 0) { firstDistinctChar = inputString[i]; break; } } return firstDistinctChar; } Check out amazing two more way - program to find first non repeating character in a string c# - three ways http://www.dotnetbull.com/2013/08/find-first-non-repeating-character-string.html

Anonym

19. März 2009

Hint: use a hash table

1

Anonym

23. Aug. 2010

public static char getFirstNonRepeatedChar(String s) { List charList = null; char nonRepeatedChar ='?'; if (s != null) { s = s.trim(); charList = new ArrayList(); for (int i=0; i