Check whether frequency of characters in a string makes Fibonacci Sequence | Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; Vector to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver code
function isFibonacci ( s ) { var m = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( m . has ( s [ i ] ) ) { m . set ( s [ i ] , m . get ( s [ i ] ) ) ; } else { m . set ( s [ i ] , 1 ) ; } } var v = [ ] ; var n = m . length ; var a = 1 , b = 1 ; var c ; v . push ( a ) ; v . push ( b ) ; for ( var i = 0 ; i < n - 2 ; i ++ ) { v . push ( a + b ) ; c = a + b ; a = b ; b = c ; } var flag = 1 ; var i = 0 ; m . forEach ( ( value , key ) => { if ( value != v [ i ] ) { flag = 0 ; } } ) ; if ( flag == 1 ) return " " ; else return " " ; } var s = " " ; document . write ( isFibonacci ( s ) ) ;