Skip to content

Commit 34cf4fc

Browse files
committed
week8
1 parent 2c66f30 commit 34cf4fc

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

longest-repeating-character-replacement/imgolden77.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ def characterReplacement(self, s: str, k: int) -> int:
2222

2323
return max_len
2424

25-
# O(N) time complexity and O(1) space complexity.
25+
# O(N) time complexity and O(1) space complexity.
26+
27+
28+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
total_count = 0
4+
5+
def expand(left, right):
6+
local_count = 0
7+
while left>=0 and right< len(s) and s[left] == s[right]:
8+
# while s[left] == s[right] and left>=0 and right< len(s) :
9+
# The above line would cause index error if left or right go out of bounds
10+
local_count += 1
11+
left -= 1
12+
right += 1
13+
return local_count
14+
15+
for i in range(len(s)):
16+
total_count += expand(i, i)
17+
total_count += expand(i, i+1)
18+
19+
return total_count
20+
21+
#Complexity Analysis
22+
#Time complexity: O(n^2) where n is the length of the input string s.
23+
#Space complexity: O(1) as we are using only a constant amount of extra space
24+
# Manacher's Algorithm can achieve O(n) time complexity for this problem.
25+

reverse-bits/imgolden77.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ def reverseBits(self, n: int) -> int:
1212

1313
return res
1414

15-
# Complexity O(1) - Time: 32 iterations, Space: O(1) no extra space used
15+
# Complexity O(1) - Time: 32 iterations, Space: O(1) no extra space used
16+

0 commit comments

Comments
 (0)