File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ Name:
2
+ Remove duplicate characters
3
+
4
+ Question:
5
+ Write a function to return a new string that contains the contents of
6
+ the input string with all duplicates removed.
7
+
8
+ Hint:
9
+ Think about using a set to keep track of already-seen characters.
10
+
11
+ Solution:
12
+ Iterate through the input string and keep already-seen characters in a
13
+ set. If a character hasn't been seen yet, then append it to an output
14
+ list. Finally convert the list into a string using "''.join()" and then
15
+ return it.
16
+
17
+ Skeleton:
18
+
19
+ def removeDups(s):
20
+ # write your solution code here
21
+
22
+ // # Example solution:
23
+ // def removeDups(s):
24
+ // seen = set()
25
+ // out = []
26
+ // for c in s:
27
+ // if c not in seen:
28
+ // out.append(c)
29
+ // seen.add(c)
30
+ // return ''.join(out)
31
+
32
+ Test:
33
+ input = "AAAABBBBB"
34
+ result = removeDups(input)
35
+
36
+ Expect:
37
+ result = "AB"
38
+
39
+ Test:
40
+ input = "Hello World"
41
+ result = removeDups(input)
42
+
43
+ Expect:
44
+ result = "Helo Wrd"
45
+
46
+ Test:
47
+ input = "Hello World"
48
+ result = removeDups(input)
49
+
50
+ Expect:
51
+ result = "Helo Wrd"
52
+
53
+ Test:
54
+ input = "alibaba"
55
+ result = removeDups(input)
56
+
57
+ Expect:
58
+ result = "alib"
59
+
60
+ Test:
61
+ input = "abcdefg"
62
+ result = removeDups(input)
63
+
64
+ Expect:
65
+ result = "abcdefg"
66
+
You can’t perform that action at this time.
0 commit comments