Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d462877

Browse files
committed
680_Valid_Palindrome_II
1 parent 660e5f1 commit d462877

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Also, there are open source implementations for basic data structs and algorithm
152152
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
153153
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
154154
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
155+
| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. |
155156
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) |
156157
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
157158
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/703_Kth_Largest_Element_in_a_Stream.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/703_Kth_Largest_Element_in_a_Stream.java) | 1. Sort and insert into right place, O(nlgn) and O(n)<br>2. k largest heap, O(nlogk) and O(n) |

java/680_Valid_Palindrome_II.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean isPalindromeRange(String s, int i, int j) {
3+
for (int k = i; k <= i + (j - i) / 2; k++) {
4+
if (s.charAt(k) != s.charAt(j - k + i)) return false;
5+
}
6+
return true;
7+
}
8+
public boolean validPalindrome(String s) {
9+
for (int i = 0; i < s.length() / 2; i++) {
10+
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
11+
// Not equal
12+
int j = s.length() - 1 - i;
13+
// delete left or right
14+
return (isPalindromeRange(s, i + 1, j) ||
15+
isPalindromeRange(s, i, j - 1));
16+
}
17+
}
18+
return true;
19+
}
20+
}

python/252_Meeting_Rooms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ def canAttendMeetings(self, intervals):
3434
for i in range(ls - 1):
3535
if intervals[i].end > intervals[i + 1].start:
3636
return False
37-
return True
37+
return True

python/680_Valid_Palindrome_II.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
# def validPalindrome(self, s):
3+
# """
4+
# :type s: str
5+
# :rtype: bool
6+
# """
7+
# def is_pali_range(i, j):
8+
# return all(s[k] == s[j - k + i] for k in range(i, j))
9+
10+
# for i in xrange(len(s) / 2):
11+
# if s[i] != s[~i]:
12+
# j = len(s) - 1 - i
13+
# return is_pali_range(i + 1, j) or is_pali_range(i, j - 1)
14+
# return True
15+
16+
# Actually we can make this solution more general
17+
def validPalindrome(self, s):
18+
return self.validPalindromeHelper(s, 0, len(s) - 1, 1)
19+
20+
def validPalindromeHelper(self, s, left, right, budget):
21+
# Note that budget can be more than 1
22+
while left < len(s) and right >= 0 and left <= right and s[left] == s[right]:
23+
left += 1
24+
right -= 1
25+
if left >= len(s) or right < 0 or left >= right:
26+
return True
27+
if budget == 0:
28+
return False
29+
budget -= 1
30+
return self.validPalindromeHelper(s, left + 1, right, budget) or self.validPalindromeHelper(s, left, right - 1, budget)

0 commit comments

Comments
 (0)