File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
JavaScript/chapter01/1.4 - PalinPerm Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Given a string, write a function to check if it is a permutation of a palindrome
2
+
3
+ const isPalindromePermutation = ( str ) => {
4
+ const strNoSpaces = str . split ( ' ' ) . join ( '' ) ;
5
+ const obj = { } ;
6
+ var oddCount = 0 ;
7
+ for ( let char of strNoSpaces ) {
8
+ if ( obj [ char ] !== undefined ) {
9
+ obj [ char ] ++ ;
10
+ } else {
11
+ obj [ char ] = 1 ;
12
+ }
13
+ }
14
+ for ( let key in obj ) {
15
+ if ( obj [ key ] % 2 ) {
16
+ oddCount ++ ;
17
+ }
18
+ if ( oddCount > 1 ) {
19
+ return false ;
20
+ }
21
+ }
22
+ return true ;
23
+ }
24
+
25
+ console . log ( isPalindromePermutation ( 'tact coa' ) === true ) ;
26
+ console . log ( isPalindromePermutation ( 'tact cooa' ) === true ) ;
27
+ console . log ( isPalindromePermutation ( 'tacr coa' ) === false ) ;
28
+ console . log ( isPalindromePermutation ( 'tactr coa' ) === false ) ;
You can’t perform that action at this time.
0 commit comments