Skip to content

Commit

Permalink
409_Longest_Palindrome and 434_Number_of_Segments_in_a_String
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyuangong committed Jul 11, 2018
1 parent 1a3165e commit 088a71d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ Remember solutions are only solutions to given problems. If you want full study
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/404_Sum_of_Left_Leaves.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/404_Sum_of_Left_Leaves.java) | 1. Recursively DFS with root.left.left and root.left.right check<br>2. The same DFS based on stack |
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/405_Convert_a_Number_to_Hexadecimal.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/405_Convert_a_Number_to_Hexadecimal.java) | [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) 1. Bit manipulate, each time handle 4 digits<br>2. Python (hex) and Java API (toHexString & Integer.toHexString) |
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/408_Valid_Word_Abbreviation.py) | Go over abbr and word, O(n) and O(1) |
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/409_Longest_Palindrome.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/409_Longest_Palindrome.java) | Length of Palindrome is always 2n or 2n + 1. So, get all possible 2*n, and choose a single one as 1 if it exists. |
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/412_Fizz_Buzz.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/412_Fizz_Buzz.java) | 1. From 1 to n, condition check<br>2. Condition check and string connect |
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/416_Partition_Equal_Subset_Sum.py) | DP, Check if sum of some elements can be half of total sum, O(total_sum / 2 * n) and O(total_sum / 2) |
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |
| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/422_Valid_Word_Square.py) | Compare row with column, O(n^2) |
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/434_Number_of_Segments_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/434_Number_of_Segments_in_a_String.java) | 1. trim &split<br>2. Find segment in place |
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit |

| # | To Understand |
Expand Down
16 changes: 16 additions & 0 deletions java/409_Longest_Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
// https://leetcode.com/problems/longest-palindrome/solution/
public int longestPalindrome(String s) {
int[] count = new int[128];
for (char c: s.toCharArray())
count[c]++;

int ans = 0;
for (int v: count) {
ans += v / 2 * 2;
if (ans % 2 == 0 && v % 2 == 1)
ans++;
}
return ans;
}
}
21 changes: 21 additions & 0 deletions java/434_Number_of_Segments_in_a_String.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
// https://leetcode.com/problems/number-of-segments-in-a-string/solution/
// public int countSegments(String s) {
// String trimmed = s.trim();
// if (trimmed.equals("")) {
// return 0;
// }
// return trimmed.split("\\s+").length;
// }
public int countSegments(String s) {
int segmentCount = 0;

for (int i = 0; i < s.length(); i++) {
if ((i == 0 || s.charAt(i-1) == ' ') && s.charAt(i) != ' ') {
segmentCount++;
}
}

return segmentCount;
}
}
22 changes: 22 additions & 0 deletions python/409_Longest_Palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
# https://leetcode.com/problems/longest-palindrome/solution/
# def longestPalindrome(self, s):
# ans = 0
# for v in collections.Counter(s).itervalues():
# ans += v / 2 * 2
# if ans % 2 == 0 and v % 2 == 1:
# ans += 1
# return ans
def longestPalindrome(self, s):
ans = 0
char_map = {}
for c in s:
char_map[c] = char_map.get(c, 0) + 1
for c in char_map.keys():
if char_map[c] % 2 == 0:
ans += char_map.pop(c)
else:
ans += char_map[c] / 2 * 2
if len(char_map) != 0:
ans += 1
return ans
16 changes: 16 additions & 0 deletions python/434_Number_of_Segments_in_a_String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
# https://leetcode.com/problems/number-of-segments-in-a-string/solution/
# def countSegments(self, s):
# """
# :type s: str
# :rtype: int
# """
# return len(s.split())

def countSegments(self, s):
segment_count = 0
for i in range(len(s)):
if (i == 0 or s[i-1] == ' ') and s[i] != ' ':
segment_count += 1

return segment_count

0 comments on commit 088a71d

Please sign in to comment.