-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathdelete_reoccurring.py
More file actions
36 lines (27 loc) · 923 Bytes
/
delete_reoccurring.py
File metadata and controls
36 lines (27 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Delete Reoccurring Characters
Given a string, delete any reoccurring characters and return the new string
containing only the first occurrence of each character.
Reference: https://en.wikipedia.org/wiki/Duplicate_removal
Complexity:
Time: O(n) where n is the length of the string
Space: O(n)
"""
from __future__ import annotations
def delete_reoccurring_characters(string: str) -> str:
"""Remove duplicate characters, keeping only the first occurrence of each.
Args:
string: The input string to process.
Returns:
A new string with duplicate characters removed.
Examples:
>>> delete_reoccurring_characters("aaabcccc")
'abc'
"""
seen_characters: set[str] = set()
output_string = ""
for char in string:
if char not in seen_characters:
seen_characters.add(char)
output_string += char
return output_string