Amazon Interview Question: Given a string find the first... | Glassdoor

Amazon

## Interview Question

Software Design Engineer Interview Seattle, WA

# Given a string find the first non-repeated character.

Tags:
data structures, programming, string manipulation

1

Hint: use a hash table

Interview Candidate on Mar 19, 2009
0

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<s.length(); i++) {
Character c = s.charAt(i);
if (!charList.contains(c)) {
} else {
charList.remove(c);
}
}
}
if (charList != null && !charList.isEmpty()) {
nonRepeatedChar = charList.get(0);
}
return nonRepeatedChar;
}

Rajiv on Aug 23, 2010
1

@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.

Kumar Manish on Feb 13, 2011
0

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.

dantepy on Apr 30, 2011
0

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;
}

AB on Jun 14, 2012
0

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

Vivek Gupta on Aug 15, 2013
1

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

Deepti Thakur on Aug 15, 2013
1

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"

Sina Yeganeh on Sep 5, 2013
0

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

}
}

poorni on Mar 23, 2015
0

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.

Anonymous on May 3, 2015