Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85d25b5

Browse files
committed
Fix docstring
1 parentbd4270f commit85d25b5

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

‎strings/is_substring_palindrome.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
MAX_LEN=1000
1+
"""
2+
This is a Python implementation for checking whether a substring is palindromic or not.
23
3-
text=""
4-
dp= [[Falseforiinrange(MAX_LEN)]foriinrange(MAX_LEN)]
4+
For testing run:
5+
python3 is_substring_palindrome.py
6+
"""
57

6-
defpreprocess(s):
8+
defpreprocess_all_substrings(text):
9+
"""Find all palindromic substrings of a string, using dynamic programming, with O(n^2) time complexity.
10+
11+
Args:
12+
text: the string to be preprocessed.
13+
Returns:
14+
A 2-dimentional matrix called ret.
15+
ret[i][j] is True, if and only if the substring text[i:j+1] is palindromic.
716
"""
8-
Preprocesses a string using dynamic programming.
9-
Time complexity: O(n^2)
10-
"""
11-
globaltext
12-
globaldp
13-
text=s
14-
n=len(s)
15-
dp= [[Falseforiinrange(n)]foriinrange(n)]
17+
n=len(text)
18+
ret= [[Falseforiinrange(n)]forjinrange(n)]
1619

1720
foriinrange(n):
18-
dp[i][i]=True
19-
20-
foriinrange(n-1):
21-
dp[i][i+1]= (text[i]==text[i+1])
21+
ret[i][i]=True
22+
ifi+1<n:
23+
ret[i][i+1]= (text[i]==text[i+1])
2224

2325
forsubstr_leninrange(2,n+1):
2426
foriinrange(n-substr_len+1):
2527
j=i+substr_len-1
26-
dp[i][j]= (text[i]==text[j]anddp[i+1][j-1])
28+
ret[i][j]= (text[i]==text[j]andret[i+1][j-1])
2729

30+
returnret
2831

29-
defis_substring_palindrome(l,r):
30-
"""
31-
Returns True if and only if the substring text[l:r] is a palindrome.
32-
Time complexity: O(1)
33-
Call preprocess function at least once, before calling this function.
32+
33+
defis_substring_palindrome(dp,l,r):
34+
"""Check whether a substring is palindromic or not, with O(1) time complexity.
35+
36+
Args:
37+
dp: a preprocessed 2-dimentional matrix.
38+
dp[i][j] has been set to True, if and only if the substring text[i:j+1] is palindromic.
39+
l: left most character of the substring index
40+
r: right most character of the substring index
41+
Returns:
42+
True, if and only if text[l:r+1] substring is palindromic.
43+
False, if and only if text[l:r+1] substring is not palindromic.
3444
"""
35-
n=len(text)
45+
n=len(dp)
3646
ifl>=rorl<0orr>n:
3747
returnFalse
3848
else:
@@ -42,20 +52,20 @@ def is_substring_palindrome(l, r):
4252
if__name__=='__main__':
4353
s="'nursesrun' and 'racecar' are some palindromes."
4454

45-
preprocess(s)
55+
dp=preprocess_all_substrings(s)
4656

4757
# Answering some queries:
48-
ifis_substring_palindrome(1,10):
58+
ifis_substring_palindrome(dp,1,10):
4959
print(s[1:10],"is a palindrome.")
5060
else:
5161
print(s[1:10],"is not a palindrome.")
5262

53-
ifis_substring_palindrome(17,24):
63+
ifis_substring_palindrome(dp,17,24):
5464
print(s[17:24],"is a palindrome.")
5565
else:
5666
print(s[17:24],"is not a palindrome.")
5767

58-
ifis_substring_palindrome(35,45):
68+
ifis_substring_palindrome(dp,35,45):
5969
print(s[35:45],"is a palindrome.")
6070
else:
6171
print(s[35:45],"is not a palindrome.")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp