-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc58_length_of_a_last_word.py
99 lines (78 loc) · 3.31 KB
/
lc58_length_of_a_last_word.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal
substring
consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.
"""
import unittest
def length_of_last_word_default(s: str) -> int:
"""
By default implementation
Time complexity: O(n), where n is the length of the input string s.
This is because the algorithm needs to iterate through the entire string
Space Complexity: O(n) as well,
because it creates a new string by stripping whitespace
and splitting the original string into words
"""
return len(s.strip().split(" ")[-1])
def length_of_last_word_by_reverse_search(s:str) -> int:
"""
By reverse search
Time complexity: O(n), where n is the length of the input string s.
This is because the algorithm needs to iterate through the entire string
Space Complexity: O(1),
because it does not create any additional data structures
that scale with the input size.
It only uses a fixed amount of space to store variables.
"""
s = s.strip()
for i in range(len(s)-1, 0, -1):
if s[i] == " ":
return len(s[i+1:])
return len(s)
class TestLengthOfLastWord(unittest.TestCase):
"""
function correctly calculates the length of the last word in the string "Hello World".
It asserts that the function returns the value 5,
which is the length of the last word "World".
"""
def test_length_of_lastword_default(self):
"""
Test the `length_of_last_word_default` function by asserting
"""
self.assertEqual(length_of_last_word_default("Hello World"), 5)
self.assertEqual(length_of_last_word_default(" fly me to the moon "), 4)
self.assertEqual(length_of_last_word_default("luffy is still joyboy"), 6)
self.assertEqual(length_of_last_word_default(" "), 0)
self.assertEqual(length_of_last_word_default(""), 0)
self.assertEqual(length_of_last_word_default("a"), 1)
self.assertEqual(length_of_last_word_default("hi"), 2)
def test_length_of_lastword_by_reverse_search(self):
"""
Test the `length_of_last_word_default` function by asserting
"""
self.assertEqual(length_of_last_word_by_reverse_search("Hello World"), 5)
self.assertEqual(length_of_last_word_by_reverse_search(" fly me to the moon "), 4)
self.assertEqual(length_of_last_word_by_reverse_search("luffy is still joyboy"), 6)
self.assertEqual(length_of_last_word_by_reverse_search(" "), 0)
self.assertEqual(length_of_last_word_by_reverse_search(""), 0)
self.assertEqual(length_of_last_word_default("a"), 1)
self.assertEqual(length_of_last_word_default("hi"), 2)
if __name__ == "__main__":
unittest.main()