Tuesday, November 07, 2017

first unique char in string

char? (s):
    if s == null:
        throw
    dq = new           //{char}
    chToNodeMap = new  //{char, node{char}}
    for i = 0, i < s.length(), i++:
        ch = s[i]
        if !chToNodeMap.hasKey(ch):
            chToNodeMap[ch] = dq.enqueue(ch)
        else:
            chNode = chToNodeMap[ch]
            if chNode != null:
                dq.removeNode(chNode)
                chToNodeMap[ch] = null
    if dq.isEmpty():
        return null
    return dq.peek()

deque:
    node enqueue(T)
    bool removeNode(node)
    T peek()
    bool isEmpty()
[Hat tip to MC]

No comments:

Post a Comment