Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Elena Feoktistova
In one iteration soltion: Java

Elena Feoktistova

Dec 29, 2023

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; } }

2

0

Comments
Comments

On this page