Created
August 17, 2022 17:25
-
-
Save ofoe-fiergbor/7d3cb3c524041eb7ab75a559532f84f2 to your computer and use it in GitHub Desktop.
Cassidoo: parensSubstring
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
public class ParensSubstring { | |
public int parensSubstring(String parens) { | |
Map<Character, Integer> map = new HashMap<>(); | |
for (char paren: parens.toCharArray()) { | |
map.put(paren, map.getOrDefault(paren, 0) + 1); | |
} | |
int result = 0; | |
for(int value: map.values()) { | |
result += value % 2 == 0 ? value : value - 1; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment