char? (s):
if s == null:
throw
countMap = new
for c in s:
countMap.tryGetValue(c, out count)
countMap[c] = count +1
for c in s:
if countMap[c] == 1:
return c
return null
char? (s):
if s == null:
throw
unique = new
repeating = new
for c in s:
if repeating.has(c):
// do nothing
else if unique.has(c):
unique.remove(c)
repeating.add(c)
else:
unique.add(c)
for c in s:
if unique.has(c):
return c
return null
char? (s):
if s == null:
throw
dq = new //{char}
chToNodeMap = new //{char, node{char}}
repeating = new //{char}
for c in s:
if repeating.has(c): // 3+ hit
// do nothing
else if chnodeMap.hasKey(c): // 2nd hit
repeating.add(c)
dq.deleteNode(chnodeMap[c])
chnodeMap.removeKey(c)
else: // 1st hit
chnodeMap[c] = dq.enqueue(c)
if dq.isEmpty():
return null
return dq.peek()
deque:
node enqueue(T)
bool deleteNode(node)
T peek()
bool isEmpty()
[Hat tip to MC]
// a a
// abc a
// aba b
// abb a
// abcbab c
// aabb null
char? (s):
if s == null:
throw
consumed = new
for i = s.length() -1, i >= 0, i--:
c = s[i]
if consumed.has(c):
if firstUniqueChar == c:
firstUniqueChar = null
else:
consumed.add(c)
firstUniqueChar = c
return firstUniqueChar
No comments:
Post a Comment