Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Purebasic 5.72
- ; Go through input string
- ; Cases
- ; Unseen character ; Pigeonhole is 0, not in ll.
- ; Seen character ; Pigeonhole is *ptr to ll element
- ; Seen again character ; Pigeonhole is 1
- ; End of input
- Procedure FirstUniqueCharacterO1n(s.s)
- NewList Found.c()
- Dim *Pigeonholes.Character(26)
- Protected L = Len(S)
- For I = 0 To L-1
- Pigeon.c = PeekC(@S+I*2) - 'a'
- P = *Pigeonholes(Pigeon)
- Select P
- Case 0 ; Not seen before
- LastElement(Found()) ; O(1)
- *Pigeonholes(Pigeon) = AddElement(Found())
- Found() = Pigeon + 'a'
- Case 1 ; Known to be a duplicate, so out of the game
- Default ; This is a new duplicate
- ChangeCurrentElement(Found(), P) ; O(1)
- DeleteElement(Found())
- *Pigeonholes(Pigeon) = 1
- EndSelect
- Next
- If FirstElement(Found()) ; O(1)
- ProcedureReturn Found()
- Else
- ProcedureReturn '_'
- EndIf
- EndProcedure
- Debug Chr(FirstUniqueCharacterO1n("abcdef")) ; a
- Debug Chr(FirstUniqueCharacterO1n("abcdefa")) ; b
- Debug Chr(FirstUniqueCharacterO1n("bbcdcdefa")) ; e
- Debug Chr(FirstUniqueCharacterO1n("aabb")) ; _
- Debug Chr(FirstUniqueCharacterO1n("")) ; _
- Debug "----------------------------------"
- Procedure FirstUniqueCharacter(s.s)
- Dim Pigeonholes.i(26)
- Protected L = Len(S)
- For I = 0 To L-1
- Pigeon.c = PeekC(@S+I*2) - 'a'
- Pigeonholes(Pigeon) + 1
- Next
- For I = 0 To L-1
- Pigeon.c = PeekC(@S+I*2) - 'a'
- If Pigeonholes(Pigeon) = 1
- ProcedureReturn Pigeon + 'a'
- EndIf
- Next
- ProcedureReturn '_'
- EndProcedure
- Debug Chr(FirstUniqueCharacter("abcdef")) ; a
- Debug Chr(FirstUniqueCharacter("abcdefa")) ; b
- Debug Chr(FirstUniqueCharacter("bbcdcdefa")) ;
- Debug Chr(FirstUniqueCharacter("aabb")) ; _
- Debug Chr(FirstUniqueCharacter("")) ; _
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement