-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path022_problem.py
More file actions
63 lines (58 loc) · 2.12 KB
/
022_problem.py
File metadata and controls
63 lines (58 loc) · 2.12 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#find the first non-repeating character in a string
def first_non_repeating_char(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
for char in s:
if char_count[char] == 1:
return char
return None
# Get input from the user
string = input("Enter a string: ")
# Find the first non-repeating character and print the result
result = first_non_repeating_char(string)
if result:
print(f"The first non-repeating character is: {result}")
else:
print("There is no non-repeating character.")
#using collections.OrderedDict
from collections import OrderedDict
def first_non_repeating_char(s):
char_count = OrderedDict()
for char in s:
char_count[char] = char_count.get(char, 0) + 1
for char, count in char_count.items():
if count == 1:
return char
return None
# Get input from the user
string = input("Enter a string: ")
# Find the first non-repeating character and print the result
result = first_non_repeating_char(string)
if result:
print(f"The first non-repeating character is: {result}")
else:
print("There is no non-repeating character.")
#using a list to maintain the order of characters
def first_non_repeating_char(s):
char_count = {}
char_order = []
for char in s:
if char not in char_count:
char_count[char] = 1
char_order.append(char)
else:
char_count[char] += 1
for char in char_order:
if char_count[char] == 1:
return char
return None
# Get input from the user
string = input("Enter a string: ")
# Find the first non-repeating character and print the result
result = first_non_repeating_char(string)
if result:
print(f"The first non-repeating character is: {result}")
else:
print("There is no non-repeating character.")
#time complexity: O(n) where n is the length of the input string, since we traverse the string twice (once to count characters and once to find the first non-repeating character). The space complexity is O(k) where k is the number of unique characters in the string, due to the character count dictionary.