Check whether frequency of characters in a string makes Fibonacci Sequence
Last Updated :
29 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "abeedddccccc" ;
map< char , int > freq;
for ( char c : s) {
freq++;
}
int f[freq.size()];
int i = 0;
for ( auto p : freq) {
f[i] = p.second;
i++;
}
int n = freq.size();
bool isFibonacci = true ;
sort(f, f + freq.size());
if (f[0] != 1 || f[1] != 1)
isFibonacci = false ;
else {
for ( int j = 2; j < n; j++) {
if (f[j] != f[j-1] + f[j-2]) {
isFibonacci = false ;
break ;
}
}
}
if (isFibonacci) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
|
Java
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 = "abeebbbccccc" ;
System.out.println(isFibonacci(s));
}
}
|
Python3
from collections import defaultdict
def isFibonacci(s):
m = defaultdict( lambda : 0 )
for i in range ( 0 , len (s)):
m[s[i]] + = 1
v = []
n = len (m)
a = b = 1
v.append(a)
v.append(b)
for i in range ( 0 , n - 2 ):
v.append(a + b)
c = a + b
a, b = b, c
flag, i = 1 , 0
for itr in sorted (m):
if m[itr] ! = v[i]:
flag = 0
break
i + = 1
if flag = = 1 :
return "YES"
else :
return "NO"
if __name__ = = "__main__" :
s = "abeebbbccccc"
print (isFibonacci(s))
|
C#
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 = "abeebbbccccc" ;
Console.WriteLine(isFibonacci(s));
}
}
|
Javascript
<script>
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 "YES" ;
else
return "NO" ;
}
var s = "abeeedd" ;
document.write( isFibonacci(s));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n is the length of the given string.
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...