Check whether frequency of characters in a string makes Fibonacci Sequence | Java program to 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
import java . util . HashMap ; import java . util . Vector ; class GFG { static String isFibonacci ( String s ) { HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) == null ? 1 : m . get ( s . charAt ( i ) ) + 1 ) ; Vector < Integer > v = new Vector < > ( ) ; int n = m . size ( ) ; int a = 1 , b = 1 ; int c ; v . add ( a ) ; v . add ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { v . add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; int i = 0 ; for ( HashMap . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( entry . getValue ( ) != v . elementAt ( i ) ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String s = " abeeedd " ; System . out . println ( isFibonacci ( s ) ) ; } }