Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Problem 4: Longest Palindrome(easy)
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
For this example:
print(sol.longestPalindrome("applepie")) # Expected output: 7
expected outcome should be 5 not 7.
A
Anonymous
· 3 years ago
According to the explanation, as the input string grows, the size of the hashmap will be at most the number of unique characters in the alphabet being used. So wouldn't that be O(1) space because the size of the hashmap does not grow linearly as the input scales?
Show 2 replies
A
Anonymous
· 3 years ago
import java.util.HashMap; public class Solution { public int longestPalindrome(String s) { int length = 0; Map<Character, Boolean> map = new HashMap<>(); for (Character ch: s.toCharArray()) { if (map.get(ch) != null) { map.remove(ch); length += 2; } else { map.put(ch, Boolean.TRUE); } } return map.isEmpty() ? length : ++length; } }
Reading Progress
0%