📜  检查字符串中字符的频率是否构成斐波那契数列

📅  最后修改于: 2022-05-13 01:57:08.494000             🧑  作者: Mango

检查字符串中字符的频率是否构成斐波那契数列

给定一个带有小写英文字母的字符串。任务是检查字符串中字符的频率是否可以排列为斐波那契数列。如果是,打印“YES”,否则打印“NO”。
笔记:

  • 频率可以以任何方式排列以形成斐波那契数列。
  • 斐波那契数列从 1 开始。即数列是1、1、2、3、5, ......

例子

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

方法:

  • 将字符串中每个字符的频率存储在映射中。让地图的大小为n   存储频率后。
  • 然后,创建一个向量并在该向量中插入斐波那契数列的前“n”个元素。
  • 然后,将向量的每个元素与地图的值进行比较。如果向量的元素和地图的值相同,则打印“YES”,否则打印“NO”。

下面是上述方法的实现:

C++
// C++ program to check whether frequency of
// characters in a string makes
// Fibonacci Sequence
 
#include 
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 m;
 
    for (int i = 0; i < s.length(); i++) {
        m[s[i]]++;
    }
 
    // Vector to store first n
    // fibonacci numbers
    vector 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;
}


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 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 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 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


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


C#
// C# program to check whether frequency of
// characters in a string makes
// Fibonacci Sequence
using System;
using System.Collections.Generic;            
     
class GFG
{
 
    // Function to check if the frequencies
    // are in Fibonacci series
    static String isFibonacci(String s)
    {
 
        // map to store the
        // frequencies of character
        int i = 0;
        Dictionary mp = new Dictionary();
        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 to store first n
        // fibonacci numbers
        List v = new List();
 
        // Get the size of the map
        int n = mp.Count;
 
        // 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 (i = 0; i < n - 2; i++)
        {
            v.Add(a + b);
            c = a + b;
            a = b;
            b = c;
        }
 
        int flag = 1;
         
        // Compare vector elements with values in Map
        foreach(KeyValuePair entry in mp)
        {
            if (entry.Value != v[i])
            {
                flag = 1;
                break;
            }
            i++;
        }
         
        if (flag == 1)
            return "YES";
        else
            return "NO";
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "abeeedd";
        Console.WriteLine(isFibonacci(s));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
YES

时间复杂度: O(n),其中 n 是给定字符串的长度。

辅助空间: O(n)