Problem:
Given a string s consisting of small English letters, find and return the first instance of a non-repeating character in it. If there is no such character, return '_'.
Example
-
For s = "abacabad", the output should be
firstNotRepeatingCharacter(s) = 'c'.There are 2 non-repeating characters in the string: 'c' and 'd'. Return c since it appears in the string first.
-
For s = "abacabaabacaba", the output should be
firstNotRepeatingCharacter(s) = '_'.There are no characters in this string that do not repeat.
-Summary-
1. Used a linear search algorithm.
2. If any number that is not in the temp_set and not exists in the rest of the string, it is the only one character.
3. If not found, return '_'
사람들이 풀어놓은 답을 봤더니 파이썬의 rindex() 라는 함수를 이용해서 정말 깔끔하게 구현할수 있었다. 오늘도 새로운 파이썬의 method를 배운다.
def firstNotRepeatingCharacter(s):
for c in s:
if s.index(c) == s.rindex(c):
return c
return '_'
모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]
'CodeSignal > Interview Practice' 카테고리의 다른 글
[Linked Lists] removeKFromList (0) | 2020.05.30 |
---|---|
[Arrays] isCryptSolution - Palantir technologies (0) | 2020.05.29 |
[Arrays] sudoku2 - Apple, Uber (0) | 2020.05.29 |
[Arrays] rotateImage - Amazon, Microsoft, Apple (0) | 2020.05.29 |
[Arrays] firstDuplicate - Google (0) | 2020.05.29 |
댓글