File tree Expand file tree Collapse file tree 1 file changed +7
-10
lines changed
JavaScript/chapter01/1.4 - PalinPerm Expand file tree Collapse file tree 1 file changed +7
-10
lines changed Original file line number Diff line number Diff line change 1
1
// Given a string, write a function to check if it is a permutation of a palindrome
2
2
3
3
const isPalindromePermutation = ( str ) => {
4
- const strNoSpaces = str . split ( ' ' ) . join ( '' ) ;
5
- const obj = { } ;
4
+ const strNoSpaces = str . replace ( / / g, '' ) ;
6
5
var oddCount = 0 ;
6
+ const frequencies = new Map ( ) ;
7
7
for ( let char of strNoSpaces ) {
8
- if ( obj [ char ] !== undefined ) {
9
- obj [ char ] ++ ;
10
- } else {
11
- obj [ char ] = 1 ;
12
- }
8
+ frequencies . set ( char , 1 + ( frequencies . get ( char ) || 0 ) ) ;
13
9
}
14
- for ( let key in obj ) {
15
- if ( obj [ key ] % 2 ) {
16
- oddCount ++ ;
10
+ for ( let frequency of frequencies . values ( ) ) {
11
+ if ( frequency % 2 === 0 ) {
12
+ continue ;
17
13
}
14
+ oddCount ++ ;
18
15
if ( oddCount > 1 ) {
19
16
return false ;
20
17
}
You can’t perform that action at this time.
0 commit comments