Symantec Interview Question: First non-repeated character ... | Glassdoor

Interview Question

Software Engineer Intern Interview(Student Candidate)

First non-repeated character in a string.

Answer

Interview Answer

2 Answers

0

String nonRepeated(String s){
if (s.length() == 0 || s.length() == 1){return s;}
HashMap map = new HashMap();
i = 0;
while (i<=s.length()-1){
if (HashMap.containsKey(s[i]) == false){
map.put(s[i], false);}
else{
map.put(s[i], true);
}
i++;
}
i = 0
while(i<=s.length()-1){
if (map.get(s[i]) == false){return s[i]}
i++;
}
return ""
}
// Please not that I did not test my code- use at your own risk

Jason Peng on Nov 11, 2012
0

correction: if (map.containsKey(s[i]) == false), let me know if there's any error. But the basic concept is correct- use a HashMap to check

Jason Peng on Nov 11, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.