-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
409_Longest_Palindrome and 434_Number_of_Segments_in_a_String
- Loading branch information
1 parent
1a3165e
commit 088a71d
Showing
5 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
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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 |
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,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 |