mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Enable ruff E741 rule (#11370)
* Enable ruff E741 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
0a9a860eb1
commit
a42eb35702
|
@ -87,12 +87,12 @@ class SegmentTree(Generic[T]):
|
||||||
p = p // 2
|
p = p // 2
|
||||||
self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1])
|
self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1])
|
||||||
|
|
||||||
def query(self, l: int, r: int) -> T | None:
|
def query(self, left: int, right: int) -> T | None:
|
||||||
"""
|
"""
|
||||||
Get range query value in log(N) time
|
Get range query value in log(N) time
|
||||||
:param l: left element index
|
:param left: left element index
|
||||||
:param r: right element index
|
:param right: right element index
|
||||||
:return: element combined in the range [l, r]
|
:return: element combined in the range [left, right]
|
||||||
|
|
||||||
>>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b)
|
>>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b)
|
||||||
>>> st.query(0, 2)
|
>>> st.query(0, 2)
|
||||||
|
@ -104,15 +104,15 @@ class SegmentTree(Generic[T]):
|
||||||
>>> st.query(2, 3)
|
>>> st.query(2, 3)
|
||||||
7
|
7
|
||||||
"""
|
"""
|
||||||
l, r = l + self.N, r + self.N
|
left, right = left + self.N, right + self.N
|
||||||
|
|
||||||
res: T | None = None
|
res: T | None = None
|
||||||
while l <= r:
|
while left <= right:
|
||||||
if l % 2 == 1:
|
if left % 2 == 1:
|
||||||
res = self.st[l] if res is None else self.fn(res, self.st[l])
|
res = self.st[left] if res is None else self.fn(res, self.st[left])
|
||||||
if r % 2 == 0:
|
if right % 2 == 0:
|
||||||
res = self.st[r] if res is None else self.fn(res, self.st[r])
|
res = self.st[right] if res is None else self.fn(res, self.st[right])
|
||||||
l, r = (l + 1) // 2, (r - 1) // 2
|
left, right = (left + 1) // 2, (right - 1) // 2
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,13 +35,13 @@ class SegmentTree:
|
||||||
"""
|
"""
|
||||||
return idx * 2 + 1
|
return idx * 2 + 1
|
||||||
|
|
||||||
def build(self, idx, l, r):
|
def build(self, idx, left, right):
|
||||||
if l == r:
|
if left == right:
|
||||||
self.st[idx] = self.A[l]
|
self.st[idx] = self.A[left]
|
||||||
else:
|
else:
|
||||||
mid = (l + r) // 2
|
mid = (left + right) // 2
|
||||||
self.build(self.left(idx), l, mid)
|
self.build(self.left(idx), left, mid)
|
||||||
self.build(self.right(idx), mid + 1, r)
|
self.build(self.right(idx), mid + 1, right)
|
||||||
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
||||||
|
|
||||||
def update(self, a, b, val):
|
def update(self, a, b, val):
|
||||||
|
@ -56,18 +56,18 @@ class SegmentTree:
|
||||||
"""
|
"""
|
||||||
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
|
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
|
||||||
|
|
||||||
def update_recursive(self, idx, l, r, a, b, val):
|
def update_recursive(self, idx, left, right, a, b, val):
|
||||||
"""
|
"""
|
||||||
update(1, 1, N, a, b, v) for update val v to [a,b]
|
update(1, 1, N, a, b, v) for update val v to [a,b]
|
||||||
"""
|
"""
|
||||||
if r < a or l > b:
|
if right < a or left > b:
|
||||||
return True
|
return True
|
||||||
if l == r:
|
if left == right:
|
||||||
self.st[idx] = val
|
self.st[idx] = val
|
||||||
return True
|
return True
|
||||||
mid = (l + r) // 2
|
mid = (left + right) // 2
|
||||||
self.update_recursive(self.left(idx), l, mid, a, b, val)
|
self.update_recursive(self.left(idx), left, mid, a, b, val)
|
||||||
self.update_recursive(self.right(idx), mid + 1, r, a, b, val)
|
self.update_recursive(self.right(idx), mid + 1, right, a, b, val)
|
||||||
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -83,17 +83,17 @@ class SegmentTree:
|
||||||
"""
|
"""
|
||||||
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
|
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
|
||||||
|
|
||||||
def query_recursive(self, idx, l, r, a, b):
|
def query_recursive(self, idx, left, right, a, b):
|
||||||
"""
|
"""
|
||||||
query(1, 1, N, a, b) for query max of [a,b]
|
query(1, 1, N, a, b) for query max of [a,b]
|
||||||
"""
|
"""
|
||||||
if r < a or l > b:
|
if right < a or left > b:
|
||||||
return -math.inf
|
return -math.inf
|
||||||
if l >= a and r <= b:
|
if left >= a and right <= b:
|
||||||
return self.st[idx]
|
return self.st[idx]
|
||||||
mid = (l + r) // 2
|
mid = (left + right) // 2
|
||||||
q1 = self.query_recursive(self.left(idx), l, mid, a, b)
|
q1 = self.query_recursive(self.left(idx), left, mid, a, b)
|
||||||
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
|
q2 = self.query_recursive(self.right(idx), mid + 1, right, a, b)
|
||||||
return max(q1, q2)
|
return max(q1, q2)
|
||||||
|
|
||||||
def show_data(self):
|
def show_data(self):
|
||||||
|
|
|
@ -66,14 +66,14 @@ class MinHeap:
|
||||||
# this is min-heapify method
|
# this is min-heapify method
|
||||||
def sift_down(self, idx, array):
|
def sift_down(self, idx, array):
|
||||||
while True:
|
while True:
|
||||||
l = self.get_left_child_idx(idx)
|
left = self.get_left_child_idx(idx)
|
||||||
r = self.get_right_child_idx(idx)
|
right = self.get_right_child_idx(idx)
|
||||||
|
|
||||||
smallest = idx
|
smallest = idx
|
||||||
if l < len(array) and array[l] < array[idx]:
|
if left < len(array) and array[left] < array[idx]:
|
||||||
smallest = l
|
smallest = left
|
||||||
if r < len(array) and array[r] < array[smallest]:
|
if right < len(array) and array[right] < array[smallest]:
|
||||||
smallest = r
|
smallest = right
|
||||||
|
|
||||||
if smallest != idx:
|
if smallest != idx:
|
||||||
array[idx], array[smallest] = array[smallest], array[idx]
|
array[idx], array[smallest] = array[smallest], array[idx]
|
||||||
|
|
|
@ -38,30 +38,30 @@ def longest_common_subsequence(x: str, y: str):
|
||||||
n = len(y)
|
n = len(y)
|
||||||
|
|
||||||
# declaring the array for storing the dp values
|
# declaring the array for storing the dp values
|
||||||
l = [[0] * (n + 1) for _ in range(m + 1)]
|
dp = [[0] * (n + 1) for _ in range(m + 1)]
|
||||||
|
|
||||||
for i in range(1, m + 1):
|
for i in range(1, m + 1):
|
||||||
for j in range(1, n + 1):
|
for j in range(1, n + 1):
|
||||||
match = 1 if x[i - 1] == y[j - 1] else 0
|
match = 1 if x[i - 1] == y[j - 1] else 0
|
||||||
|
|
||||||
l[i][j] = max(l[i - 1][j], l[i][j - 1], l[i - 1][j - 1] + match)
|
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + match)
|
||||||
|
|
||||||
seq = ""
|
seq = ""
|
||||||
i, j = m, n
|
i, j = m, n
|
||||||
while i > 0 and j > 0:
|
while i > 0 and j > 0:
|
||||||
match = 1 if x[i - 1] == y[j - 1] else 0
|
match = 1 if x[i - 1] == y[j - 1] else 0
|
||||||
|
|
||||||
if l[i][j] == l[i - 1][j - 1] + match:
|
if dp[i][j] == dp[i - 1][j - 1] + match:
|
||||||
if match == 1:
|
if match == 1:
|
||||||
seq = x[i - 1] + seq
|
seq = x[i - 1] + seq
|
||||||
i -= 1
|
i -= 1
|
||||||
j -= 1
|
j -= 1
|
||||||
elif l[i][j] == l[i - 1][j]:
|
elif dp[i][j] == dp[i - 1][j]:
|
||||||
i -= 1
|
i -= 1
|
||||||
else:
|
else:
|
||||||
j -= 1
|
j -= 1
|
||||||
|
|
||||||
return l[m][n], seq
|
return dp[m][n], seq
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -7,14 +7,14 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
def ceil_index(v, l, r, key):
|
def ceil_index(v, left, right, key):
|
||||||
while r - l > 1:
|
while right - left > 1:
|
||||||
m = (l + r) // 2
|
middle = (left + right) // 2
|
||||||
if v[m] >= key:
|
if v[middle] >= key:
|
||||||
r = m
|
right = middle
|
||||||
else:
|
else:
|
||||||
l = m
|
left = middle
|
||||||
return r
|
return right
|
||||||
|
|
||||||
|
|
||||||
def longest_increasing_subsequence_length(v: list[int]) -> int:
|
def longest_increasing_subsequence_length(v: list[int]) -> int:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Finding Articulation Points in Undirected Graph
|
# Finding Articulation Points in Undirected Graph
|
||||||
def compute_ap(l):
|
def compute_ap(graph):
|
||||||
n = len(l)
|
n = len(graph)
|
||||||
out_edge_count = 0
|
out_edge_count = 0
|
||||||
low = [0] * n
|
low = [0] * n
|
||||||
visited = [False] * n
|
visited = [False] * n
|
||||||
|
@ -12,7 +12,7 @@ def compute_ap(l):
|
||||||
visited[at] = True
|
visited[at] = True
|
||||||
low[at] = at
|
low[at] = at
|
||||||
|
|
||||||
for to in l[at]:
|
for to in graph[at]:
|
||||||
if to == parent:
|
if to == parent:
|
||||||
pass
|
pass
|
||||||
elif not visited[to]:
|
elif not visited[to]:
|
||||||
|
@ -41,7 +41,7 @@ def compute_ap(l):
|
||||||
|
|
||||||
|
|
||||||
# Adjacency list of graph
|
# Adjacency list of graph
|
||||||
data = {
|
graph = {
|
||||||
0: [1, 2],
|
0: [1, 2],
|
||||||
1: [0, 2],
|
1: [0, 2],
|
||||||
2: [0, 1, 3, 5],
|
2: [0, 1, 3, 5],
|
||||||
|
@ -52,4 +52,4 @@ data = {
|
||||||
7: [6, 8],
|
7: [6, 8],
|
||||||
8: [5, 7],
|
8: [5, 7],
|
||||||
}
|
}
|
||||||
compute_ap(data)
|
compute_ap(graph)
|
||||||
|
|
|
@ -37,7 +37,7 @@ class Dinic:
|
||||||
# Here we calculate the flow that reaches the sink
|
# Here we calculate the flow that reaches the sink
|
||||||
def max_flow(self, source, sink):
|
def max_flow(self, source, sink):
|
||||||
flow, self.q[0] = 0, source
|
flow, self.q[0] = 0, source
|
||||||
for l in range(31): # l = 30 maybe faster for random data
|
for l in range(31): # l = 30 maybe faster for random data # noqa: E741
|
||||||
while True:
|
while True:
|
||||||
self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q)
|
self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q)
|
||||||
qi, qe, self.lvl[source] = 0, 1, 1
|
qi, qe, self.lvl[source] = 0, 1, 1
|
||||||
|
|
|
@ -309,9 +309,9 @@ class SmoSVM:
|
||||||
# calculate L and H which bound the new alpha2
|
# calculate L and H which bound the new alpha2
|
||||||
s = y1 * y2
|
s = y1 * y2
|
||||||
if s == -1:
|
if s == -1:
|
||||||
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1)
|
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1) # noqa: E741
|
||||||
else:
|
else:
|
||||||
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1)
|
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1) # noqa: E741
|
||||||
if l == h:
|
if l == h:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ def calculate_pi(limit: int) -> str:
|
||||||
t = 1
|
t = 1
|
||||||
k = 1
|
k = 1
|
||||||
n = 3
|
n = 3
|
||||||
l = 3
|
m = 3
|
||||||
|
|
||||||
decimal = limit
|
decimal = limit
|
||||||
counter = 0
|
counter = 0
|
||||||
|
@ -65,11 +65,11 @@ def calculate_pi(limit: int) -> str:
|
||||||
q *= 10
|
q *= 10
|
||||||
r = nr
|
r = nr
|
||||||
else:
|
else:
|
||||||
nr = (2 * q + r) * l
|
nr = (2 * q + r) * m
|
||||||
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
|
nn = (q * (7 * k) + 2 + (r * m)) // (t * m)
|
||||||
q *= k
|
q *= k
|
||||||
t *= l
|
t *= m
|
||||||
l += 2
|
m += 2
|
||||||
k += 1
|
k += 1
|
||||||
n = nn
|
n = nn
|
||||||
r = nr
|
r = nr
|
||||||
|
|
|
@ -44,11 +44,11 @@ def function(expansion, s0, s1, key, message):
|
||||||
right = message[4:]
|
right = message[4:]
|
||||||
temp = apply_table(right, expansion)
|
temp = apply_table(right, expansion)
|
||||||
temp = xor(temp, key)
|
temp = xor(temp, key)
|
||||||
l = apply_sbox(s0, temp[:4])
|
left_bin_str = apply_sbox(s0, temp[:4])
|
||||||
r = apply_sbox(s1, temp[4:])
|
right_bin_str = apply_sbox(s1, temp[4:])
|
||||||
l = "0" * (2 - len(l)) + l
|
left_bin_str = "0" * (2 - len(left_bin_str)) + left_bin_str
|
||||||
r = "0" * (2 - len(r)) + r
|
right_bin_str = "0" * (2 - len(right_bin_str)) + right_bin_str
|
||||||
temp = apply_table(l + r, p4_table)
|
temp = apply_table(left_bin_str + right_bin_str, p4_table)
|
||||||
temp = xor(left, temp)
|
temp = xor(left, temp)
|
||||||
return temp + right
|
return temp + right
|
||||||
|
|
||||||
|
|
|
@ -35,37 +35,47 @@ def solution():
|
||||||
70600674
|
70600674
|
||||||
"""
|
"""
|
||||||
with open(os.path.dirname(__file__) + "/grid.txt") as f:
|
with open(os.path.dirname(__file__) + "/grid.txt") as f:
|
||||||
l = []
|
grid = []
|
||||||
for _ in range(20):
|
for _ in range(20):
|
||||||
l.append([int(x) for x in f.readline().split()])
|
grid.append([int(x) for x in f.readline().split()])
|
||||||
|
|
||||||
maximum = 0
|
maximum = 0
|
||||||
|
|
||||||
# right
|
# right
|
||||||
for i in range(20):
|
for i in range(20):
|
||||||
for j in range(17):
|
for j in range(17):
|
||||||
temp = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
|
temp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]
|
||||||
if temp > maximum:
|
if temp > maximum:
|
||||||
maximum = temp
|
maximum = temp
|
||||||
|
|
||||||
# down
|
# down
|
||||||
for i in range(17):
|
for i in range(17):
|
||||||
for j in range(20):
|
for j in range(20):
|
||||||
temp = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
|
temp = grid[i][j] * grid[i + 1][j] * grid[i + 2][j] * grid[i + 3][j]
|
||||||
if temp > maximum:
|
if temp > maximum:
|
||||||
maximum = temp
|
maximum = temp
|
||||||
|
|
||||||
# diagonal 1
|
# diagonal 1
|
||||||
for i in range(17):
|
for i in range(17):
|
||||||
for j in range(17):
|
for j in range(17):
|
||||||
temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
|
temp = (
|
||||||
|
grid[i][j]
|
||||||
|
* grid[i + 1][j + 1]
|
||||||
|
* grid[i + 2][j + 2]
|
||||||
|
* grid[i + 3][j + 3]
|
||||||
|
)
|
||||||
if temp > maximum:
|
if temp > maximum:
|
||||||
maximum = temp
|
maximum = temp
|
||||||
|
|
||||||
# diagonal 2
|
# diagonal 2
|
||||||
for i in range(17):
|
for i in range(17):
|
||||||
for j in range(3, 20):
|
for j in range(3, 20):
|
||||||
temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
|
temp = (
|
||||||
|
grid[i][j]
|
||||||
|
* grid[i + 1][j - 1]
|
||||||
|
* grid[i + 2][j - 2]
|
||||||
|
* grid[i + 3][j - 3]
|
||||||
|
)
|
||||||
if temp > maximum:
|
if temp > maximum:
|
||||||
maximum = temp
|
maximum = temp
|
||||||
return maximum
|
return maximum
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
lint.ignore = [ # `ruff rule S101` for a description of that rule
|
lint.ignore = [ # `ruff rule S101` for a description of that rule
|
||||||
"B904", # Within an `except` clause, raise exceptions with `raise ... from err` -- FIX ME
|
"B904", # Within an `except` clause, raise exceptions with `raise ... from err` -- FIX ME
|
||||||
"B905", # `zip()` without an explicit `strict=` parameter -- FIX ME
|
"B905", # `zip()` without an explicit `strict=` parameter -- FIX ME
|
||||||
"E741", # Ambiguous variable name 'l' -- FIX ME
|
|
||||||
"EM101", # Exception must not use a string literal, assign to variable first
|
"EM101", # Exception must not use a string literal, assign to variable first
|
||||||
"EXE001", # Shebang is present but file is not executable -- DO NOT FIX
|
"EXE001", # Shebang is present but file is not executable -- DO NOT FIX
|
||||||
"G004", # Logging statement uses f-string
|
"G004", # Logging statement uses f-string
|
||||||
|
|
|
@ -28,12 +28,12 @@ def jaro_winkler(str1: str, str2: str) -> float:
|
||||||
def get_matched_characters(_str1: str, _str2: str) -> str:
|
def get_matched_characters(_str1: str, _str2: str) -> str:
|
||||||
matched = []
|
matched = []
|
||||||
limit = min(len(_str1), len(_str2)) // 2
|
limit = min(len(_str1), len(_str2)) // 2
|
||||||
for i, l in enumerate(_str1):
|
for i, char in enumerate(_str1):
|
||||||
left = int(max(0, i - limit))
|
left = int(max(0, i - limit))
|
||||||
right = int(min(i + limit + 1, len(_str2)))
|
right = int(min(i + limit + 1, len(_str2)))
|
||||||
if l in _str2[left:right]:
|
if char in _str2[left:right]:
|
||||||
matched.append(l)
|
matched.append(char)
|
||||||
_str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}"
|
_str2 = f"{_str2[0:_str2.index(char)]} {_str2[_str2.index(char) + 1:]}"
|
||||||
|
|
||||||
return "".join(matched)
|
return "".join(matched)
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,9 @@ def palindromic_string(input_string: str) -> str:
|
||||||
|
|
||||||
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
||||||
positions are actual input characters.
|
positions are actual input characters.
|
||||||
2. for each character in new_string it find corresponding length and store the
|
2. for each character in new_string it find corresponding length and
|
||||||
length and l,r to store previously calculated info.(please look the explanation
|
store the length and left,right to store previously calculated info.
|
||||||
for details)
|
(please look the explanation for details)
|
||||||
|
|
||||||
3. return corresponding output_string by removing all "|"
|
3. return corresponding output_string by removing all "|"
|
||||||
"""
|
"""
|
||||||
|
@ -29,7 +29,7 @@ def palindromic_string(input_string: str) -> str:
|
||||||
|
|
||||||
# we will store the starting and ending of previous furthest ending palindromic
|
# we will store the starting and ending of previous furthest ending palindromic
|
||||||
# substring
|
# substring
|
||||||
l, r = 0, 0
|
left, right = 0, 0
|
||||||
|
|
||||||
# length[i] shows the length of palindromic substring with center i
|
# length[i] shows the length of palindromic substring with center i
|
||||||
length = [1 for i in range(len(new_input_string))]
|
length = [1 for i in range(len(new_input_string))]
|
||||||
|
@ -37,7 +37,7 @@ def palindromic_string(input_string: str) -> str:
|
||||||
# for each character in new_string find corresponding palindromic string
|
# for each character in new_string find corresponding palindromic string
|
||||||
start = 0
|
start = 0
|
||||||
for j in range(len(new_input_string)):
|
for j in range(len(new_input_string)):
|
||||||
k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
|
k = 1 if j > right else min(length[left + right - j] // 2, right - j + 1)
|
||||||
while (
|
while (
|
||||||
j - k >= 0
|
j - k >= 0
|
||||||
and j + k < len(new_input_string)
|
and j + k < len(new_input_string)
|
||||||
|
@ -47,11 +47,11 @@ def palindromic_string(input_string: str) -> str:
|
||||||
|
|
||||||
length[j] = 2 * k - 1
|
length[j] = 2 * k - 1
|
||||||
|
|
||||||
# does this string is ending after the previously explored end (that is r) ?
|
# does this string is ending after the previously explored end (that is right) ?
|
||||||
# if yes the update the new r to the last index of this
|
# if yes the update the new right to the last index of this
|
||||||
if j + k - 1 > r:
|
if j + k - 1 > right:
|
||||||
l = j - k + 1
|
left = j - k + 1
|
||||||
r = j + k - 1
|
right = j + k - 1
|
||||||
|
|
||||||
# update max_length and start position
|
# update max_length and start position
|
||||||
if max_length < length[j]:
|
if max_length < length[j]:
|
||||||
|
@ -78,8 +78,9 @@ if __name__ == "__main__":
|
||||||
consider the string for which we are calculating the longest palindromic substring is
|
consider the string for which we are calculating the longest palindromic substring is
|
||||||
shown above where ... are some characters in between and right now we are calculating
|
shown above where ... are some characters in between and right now we are calculating
|
||||||
the length of palindromic substring with center at a5 with following conditions :
|
the length of palindromic substring with center at a5 with following conditions :
|
||||||
i) we have stored the length of palindromic substring which has center at a3 (starts at
|
i) we have stored the length of palindromic substring which has center at a3
|
||||||
l ends at r) and it is the furthest ending till now, and it has ending after a6
|
(starts at left ends at right) and it is the furthest ending till now,
|
||||||
|
and it has ending after a6
|
||||||
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
|
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
|
||||||
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
|
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
|
||||||
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
|
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
|
||||||
|
@ -98,11 +99,11 @@ so we can say that palindrome at center a5 is at least as long as palindrome at
|
||||||
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
|
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
|
||||||
so finally ..
|
so finally ..
|
||||||
|
|
||||||
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)
|
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), right-a5)
|
||||||
where a3 lies from l to r and we have to keep updating that
|
where a3 lies from left to right and we have to keep updating that
|
||||||
|
|
||||||
and if the a5 lies outside of l,r boundary we calculate length of palindrome with
|
and if the a5 lies outside of left,right boundary we calculate length of palindrome with
|
||||||
bruteforce and update l,r.
|
bruteforce and update left,right.
|
||||||
|
|
||||||
it gives the linear time complexity just like z-function
|
it gives the linear time complexity just like z-function
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user