Skip to content

Commit

Permalink
Raining day. I did not see any sunshine until 11pm we had a video cha…
Browse files Browse the repository at this point in the history
…t. Good night honey
  • Loading branch information
Meng Wei authored and Meng Wei committed Jan 9, 2018
1 parent 5392e94 commit 074bbaf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 125-valid-palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#EASY
# The tricky part is to do this task only in one pass
# If you want to do it in 2 passes then you will get timeout.
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if s == "":
return True
j = len(s)-1
i = 0

while i < j:
if not s[i].isalnum():
i += 1
continue
if not s[j].isalnum():
j -= 1
continue
if s[i].lower() != s[j].lower():
return False
else:
i+=1
j-=1

return True
26 changes: 26 additions & 0 deletions 234-palindrome-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# In the discussion, people are arguing about "is reverse() considered as o(1)"
# Yes I think so...
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
slow = head
fast = head
rev = None
while fast and fast.next:
fast = fast.next.next
temp = slow.next
slow.next = rev
rev = slow
slow = temp
if fast:
slow = slow.next

while slow!=None and rev!=None:
if(rev.val != slow.val):
return False
rev = rev.next
slow = slow.next
return True
23 changes: 23 additions & 0 deletions 686-repeated-string-match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Easy
# T: o(n)
# S: o(n)
class Solution(object):
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
Let n be the answer, the minimum number of times A has to be repeated.
For B to be inside A, A has to be repeated sufficient times such that it is at least as long as B (or one more), hence we can conclude that the theoretical lower bound for the answer would be length of B / length of A.
Let x be the theoretical lower bound, which is ceil(len(B)/len(A)).
The answer n can only be x or x + 1 (in the case where len(B) is a multiple of len(A) like in A = "abcd" and B = "cdabcdab") and not more. Because if B is already in A * n, B is definitely in A * (n + 1).
Hence we only need to check whether B in A * x or B in A * (x + 1), and if both are not possible return -1.
"""
x = int(math.ceil(1.0*len(B)/len(A)))
if B in A*x:
return x
elif B in A*(x+1):
return x+1
else:
return -1

0 comments on commit 074bbaf

Please sign in to comment.