Check whether frequency of characters in a string makes Fibonacci Sequence
Given a string with lowercase English alphabets. The task is to check whether the frequency of the characters in the string can be arranged as a Fibonacci series. If yes, print “YES”, otherwise print “NO”.
Note:
- Frequencies can be arranged in any way to form Fibonacci Series.
- The Fibonacci Series starts from 1. That is the series is 1,1,2,3,5,…..
Examples:
Input : str = "abeeedd"
Output : YES
Frequency of 'a' => 1
Frequency of 'b' => 1
Frequency of 'e' => 3
Frequency of 'd' => 2
These frequencies are first 4 terms of
Fibonacci series => {1, 1, 2, 3}
Input : str = "dzzddz"
Output : NO
Frequencies are not in Fibonacci series
Approach:
- Store the frequencies of each character of the string in a map. Let the size of the map be
after storing frequencies. - Then, make a vector and insert first ‘n’ elements of the Fibonacci series in this vector.
- Then, compare each element of the vector with values of the map. If both elements of vector and values of the map are same, print ‘YES’, otherwise print ‘NO’.
Below is the implementation of the above approach:
C++
// C++ program to check whether frequency of // characters in a string makes // Fibonacci Sequence #include <bits/stdc++.h> using namespace std; // Function to check if the frequencies // are in Fibonacci series string isFibonacci(string s) { // map to store the // frequencies of character map<char, int> m; for (int i = 0; i < s.length(); i++) { m[s[i]]++; } // Vector to store first n // fibonacci numbers vector<int> v; // Get the size of the map int n = m.size(); // a and b are first and second terms of // fibonacci series int a = 1, b = 1; int c; v.push_back(a); v.push_back(b); // vector v contains elements of fibonacci series for (int i = 0; i < n - 2; i++) { v.push_back(a + b); c = a + b; a = b; b = c; } int flag = 1; int i = 0; // Compare vector elements with values in Map for (auto itr = m.begin(); itr != m.end(); itr++) { if (itr->second != v[i]) { flag = 0; break; } i++; } if (flag == 1) return "YES"; else return "NO"; } // Driver code int main() { string s = "abeeedd"; cout << isFibonacci(s); return 0; } |
chevron_right
filter_none
Java
// Java program to check whether frequency of // characters in a string makes // Fibonacci Sequence import java.util.HashMap; import java.util.Vector; class GFG { // Function to check if the frequencies // are in Fibonacci series static String isFibonacci(String s) { // map to store the // frequencies of character 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 to store first n // fibonacci numbers Vector<Integer> v = new Vector<>(); // Get the size of the map int n = m.size(); // a and b are first and second terms of // fibonacci series int a = 1, b = 1; int c; v.add(a); v.add(b); // vector v contains elements of // fibonacci series 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; // Compare vector elements with values in Map 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"; } // Driver Code public static void main(String[] args) { String s = "abeeedd"; System.out.println(isFibonacci(s)); } } // This code is contributed by // sanjeev2552 |
chevron_right
filter_none
Python3
# Python3 program to check whether the frequency # of characters in a string make Fibonacci Sequence from collections import defaultdict # Function to check if the frequencies # are in Fibonacci series def isFibonacci(s): # map to store the frequencies of character m = defaultdict(lambda:0) for i in range(0, len(s)): m[s[i]] += 1 # Vector to store first n fibonacci numbers v = [] # Get the size of the map n = len(m) # a and b are first and second # terms of fibonacci series a = b = 1 v.append(a) v.append(b) # vector v contains elements of # fibonacci series for i in range(0, n - 2): v.append(a + b) c = a + b a, b = b, c flag, i = 1, 0 # Compare vector elements with values in Map for itr in sorted(m): if m[itr] != v[i]: flag = 0 break i += 1 if flag == 1: return "YES" else: return "NO" # Driver code if __name__ == "__main__": s = "abeeedd" print(isFibonacci(s)) # This code is contributed by Rituraj Jain |
chevron_right
filter_none
Output:
YES
Recommended Posts:
- String with frequency of characters in Lucas Sequence
- Check if a string has all characters with same frequency with one variation allowed
- Given two strings check which string makes a palindrome first
- Check if frequency of all characters can become same by one removal
- Program to check if all characters have even frequency
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Check if frequency of characters are in Recaman Series
- Remove even frequency characters from the string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find Bit whose minimum sequence flips makes all bits same
- Check if the frequency of any character is more than half the length of the string
- Check whether second string can be formed from characters of first string
- Check if there exists any sub-sequence in a string which is not palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



