Check whether frequency of characters in a string makes Fibonacci Sequence | C # 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 ; List 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
using System ; using System . Collections . Generic ; class GFG { static String isFibonacci ( String s ) { int i = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { var val = mp [ s [ i ] ] ; mp . Remove ( s [ i ] ) ; mp . Add ( s [ i ] , val + 1 ) ; } else { mp . Add ( s [ i ] , 1 ) ; } } List < int > v = new List < int > ( ) ; int n = mp . Count ; int a = 1 , b = 1 ; int c ; v . Add ( a ) ; v . Add ( b ) ; for ( i = 0 ; i < n - 2 ; i ++ ) { v . Add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value != v [ i ] ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void Main ( String [ ] args ) { String s = " abeeedd " ; Console . WriteLine ( isFibonacci ( s ) ) ; } }