Skip to content

Commit 92bd9dc

Browse files
committed
Adding Simple Solution for Problem 647 - Palindromic Substrings
1 parent 4b22f12 commit 92bd9dc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 647
6+
## Problem Name: Palindromic Substrings
7+
##===================================
8+
#
9+
#Given a string, your task is to count how many palindromic substrings in this string.
10+
#
11+
#The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
12+
#
13+
#Example 1:
14+
#
15+
#Input: "abc"
16+
#Output: 3
17+
#Explanation: Three palindromic strings: "a", "b", "c".
18+
#
19+
#Example 2:
20+
#
21+
#Input: "aaa"
22+
#Output: 6
23+
#Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
24+
class Solution:
25+
def countSubstrings(self, s):
26+
tmp = 0 #Initialize tmp
27+
for i in range(2*len(s) + 1): #Loop through string with given range
28+
l = r = i // 2 #Initialize left and right which is i // 2 int value
29+
if i % 2 == 1: #Condition-check: If i is not even
30+
r += 1 #Update r by increasing 1
31+
while l >= 0 and r < len(s) and s[l] == s[r]: #Loop till the condition met
32+
tmp += 1 #Update tmp by increasing 1
33+
l -= 1 #Update l by decreasing 1
34+
r += 1 #Update r by increasing 1
35+
return tmp #We return tmp which is count

0 commit comments

Comments
 (0)