-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raining day. I did not see any sunshine until 11pm we had a video cha…
…t. Good night honey
- Loading branch information
Meng Wei
authored and
Meng Wei
committed
Jan 9, 2018
1 parent
5392e94
commit 074bbaf
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |