text: Check if a string has all characters with same frequency with one variation allowed | To check a string S can be converted to a variation string ; Run loop form 0 to length of string ; declaration of variables ; if first is true than countOfVal1 increase ; if second is true than countOfVal2 increase ; Driver code
code: function checkForVariation ( str ) { if ( str == null str . length == 0 ) { return true ; } let map = new Map ( ) ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ! map . has ( str [ i ] ) ) map . set ( str [ i ] , 0 ) ; map . set ( str [ i ] , map . get ( str [ i ] ) + 1 ) ; } let first = true , second = true ; let val1 = 0 , val2 = 0 ; let countOfVal1 = 0 , countOfVal2 = 0 ; for ( let [ key , value ] of map . entries ( ) ) { let i = value ; if ( first ) { val1 = i ; first = false ; countOfVal1 ++ ; continue ; } if ( i == val1 ) { countOfVal1 ++ ; continue ; } if ( second ) { val2 = i ; countOfVal2 ++ ; second = false ; continue ; } if ( i == val2 ) { countOfVal2 ++ ; continue ; } return false ; } if ( countOfVal1 > 1 && countOfVal2 > 1 ) { return false ; } else { return true ; } } document . write ( checkForVariation ( " " ) ) ;