Skip to content

Commit

Permalink
1115 revise
Browse files Browse the repository at this point in the history
  • Loading branch information
youhusky committed Nov 16, 2017
1 parent 6c80c0e commit ee4db68
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 020. Valid Parentheses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Solution(object):
def isValid(self, s):
"""
O(N)
O(1)
O(N)
:type s: str
:rtype: bool
"""
Expand Down
35 changes: 35 additions & 0 deletions 647. Palindromic Substrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Given a string, your task is to count how many palindromic substrings in this string.

# The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

# Example 1:
# Input: "abc"
# Output: 3
# Explanation: Three palindromic strings: "a", "b", "c".
# Example 2:
# Input: "aaa"
# Output: 6
# Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
# Note:
# The input string length won't exceed 1000.
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
if not s:
return 0
if len(s) == 1:
return 1

count = [0]
for i in range(len(s)):
self.check(s,i,i,count) # abcddcba -- # abcdcba
self.check(s,i,i+1, count)
return count[0]
def check(self, s, l, r, count):
while l>=0 and r < len(s) and s[l] == s[r]:
count[0] += 1
l -= 1
r += 1

0 comments on commit ee4db68

Please sign in to comment.