📌  相关文章
📜  检查字符频率是否在Recaman系列中

📅  最后修改于: 2021-04-24 18:52:08             🧑  作者: Mango

给定字符串小写字母。任务是检查以任何可能的方式排列后,此字符串中字母的频率是否形成Recaman序列(不包括第一项)。

如果顺序打印,则打印“是”,否则输出“否”。

Recaman序列的几个起始术语是:

注意:由于Recaman序列的第一项为零,因此不予考虑。

例子:

Input  : str = "dddeweecceee"
Output : YES
Frequency of 'd' => 3
Frequency of 'e' => 6
Frequency of 'w' => 1
Frequency of 'c' => 2
These frequencies form the first 4 terms of 
Recaman's sequence => {1, 3, 6, 2}

Input : str = "geeksforgeeks"
Output : NO

方法:

  • 遍历字符串并将字符的频率存储在映射中。存储频率后,让地图的大小为N。
  • 现在,创建一个数组,并在其中插入Recaman序列的前N个元素。
  • 现在,遍历数组并检查数组的元素是否在map中作为键存在(不包括对零的检查)
  • 如果映射中存在数组的每个元素,则输出“是”,否则输出“否”。

下面是上述方法的实现:

C++
// C++ program to check whether frequency of
// characters in a string makes
// Recaman Sequence
  
#include 
using namespace std;
  
// Function to fill the array with first N numbers
// from Recaman's Sequence
int recaman(int arr[], int n)
{
    // First term of the sequence is always 0
    arr[0] = 0;
  
    // Fill remaining terms using recursive
    // formula
    for (int i = 1; i <= n; i++) {
        int temp = arr[i - 1] - i;
        int j;
  
        for (j = 0; j < i; j++) {
  
            // If arr[i-1] - i is negative or
            // already exists.
            if ((arr[j] == temp) || temp < 0) {
                temp = arr[i - 1] + i;
                break;
            }
        }
  
        arr[i] = temp;
    }
}
  
// Function to check if the frequencies
// are in Recaman series
string isRecaman(string s)
{
    // Store frequencies of characters
    unordered_map m;
    for (int i = 0; i < s.length(); i++) 
        m[s[i]]++;    
  
    // Get the size of the map
    int n = m.size();
  
    int arr[n + 1];
    recaman(arr, n);
  
    int flag = 1;
  
    // Compare vector elements with values in Map
    for (auto itr = m.begin(); itr != m.end(); itr++) {
  
        int found = 0;
  
        for (int j = 1; j <= n; j++) {
            if (itr->second == arr[j]) {
                found = 1;
                break;
            }
        }
  
        if (found == 0) {
            flag = 0;
            break;
        }
    }
  
    if (flag == 1)
        return "YES";
    else
        return "NO";
}
  
// Driver code
int main()
{
    string s = "geeekkkkkkss";
    cout << isRecaman(s);
    return 0;
}


Java
// Java program to check whether frequency of 
// characters in a string makes Recaman Sequence 
import java.util.HashMap;
import java.util.Map;
  
class GfG
{
  
    // Function to fill the array with first 
    // N numbers from Recaman's Sequence 
    static void recaman(int arr[], int n) 
    { 
        // First term of the sequence is always 0 
        arr[0] = 0; 
      
        // Fill remaining terms using 
        // recursive formula 
        for (int i = 1; i <= n; i++)
        { 
            int temp = arr[i - 1] - i; 
      
            for (int j = 0; j < i; j++)
            { 
      
                // If arr[i-1] - i is negative or 
                // already exists. 
                if ((arr[j] == temp) || temp < 0)
                { 
                    temp = arr[i - 1] + i; 
                    break; 
                } 
            } 
      
            arr[i] = temp; 
        } 
    } 
      
    // Function to check if the frequencies 
    // are in Recaman series 
    static String isRecaman(String s) 
    { 
        // Store frequencies of characters 
        HashMap  m = new HashMap<>(); 
        for (int i = 0; i < s.length(); i++) 
              
            if (m.containsKey(s.charAt(i)))
                m.put(s.charAt(i), m.get(s.charAt(i))+1);
            else
                m.put(s.charAt(i), 1);
      
        // Get the size of the map 
        int n = m.size(); 
      
        int arr[] = new int[n + 1]; 
        recaman(arr, n); 
      
        int flag = 1; 
      
        // Compare vector elements with values in Map 
        for (Map.Entry mapEle : m.entrySet()) 
        { 
      
            int found = 0; 
      
            for (int j = 1; j <= n; j++)
            { 
                if ((int)mapEle.getValue() == arr[j]) 
                { 
                    found = 1; 
                    break; 
                } 
            } 
      
            if (found == 0) 
            { 
                flag = 0; 
                break; 
            } 
        } 
      
        if (flag == 1) 
            return "YES"; 
        else
            return "NO"; 
    } 
  
    // Driver code
    public static void main(String []args)
    {
        String s = "geeekkkkkkss"; 
        System.out.println(isRecaman(s));
    }
}
  
// This code is contributed by Rituraj Jain


Python3
# Python3 program to check whether 
# frequency of characters in a string 
# makes Recaman Sequence 
  
# Function to fill the array with first 
# N numbers from Recaman's Sequence 
def recaman(arr, n) :
  
    # First term of the sequence 
    # is always 0 
    arr[0] = 0; 
  
    # Fill remaining terms using 
    # recursive formula 
    for i in range(1, n + 1) :
        temp = arr[i - 1] - i; 
  
        for j in range(i) :
  
            # If arr[i-1] - i is negative 
            # or already exists. 
            if ((arr[j] == temp) or temp < 0) :
                temp = arr[i - 1] + i; 
                break;
                  
        arr[i] = temp; 
  
# Function to check if the frequencies 
# are in Recaman series 
def isRecaman(s) :
      
    # Store frequencies of characters 
    m = dict.fromkeys(list(s), 0); 
      
    for i in range(len(s)) :
        m[s[i]] += 1; 
  
    # Get the size of the map 
    n = len(m); 
  
    arr = [0] * (n + 1); 
    recaman(arr, n); 
  
    flag = 1; 
  
    # Compare vector elements with 
    # values in Map 
    for keys in m.keys() :
  
        found = 0; 
  
        for j in range(1, n + 1) :
            if (m[keys] == arr[j]) :
                found = 1; 
                break; 
          
        if (found == 0) :
            flag = 0; 
            break; 
  
    if (flag == 1) :
        return "YES"; 
    else :
        return "NO"; 
  
# Driver code 
if __name__ == "__main__" : 
  
    s = "geeekkkkkkss"; 
      
    print(isRecaman(s)); 
  
# This code is contributed by Ryuga


C#
// C# program to check whether frequency of 
// characters in a string makes Recaman Sequence
using System;
using System.Collections.Generic;
  
class GFG
{
    // Function to fill the array with first 
    // N numbers from Recaman's Sequence
    public static void recaman(int[] arr, int n)
    {
        // First term of the sequence is always 0
        arr[0] = 0;
          
        // Fill remaining terms using 
        // recursive formula
        for (int i = 1; i <= n; i++)
        {
            int temp = arr[i - 1] - i;
            for (int j = 0; j < i; j++)
            {
                // If arr[i-1] - i is negative or 
                // already exists.
                if ((arr[j] == temp) || temp < 0)
                {
                    temp = arr[i - 1] + i;
                    break;
                }
            }
  
            arr[i] = temp;
        }
    }
  
    // Function to check if the frequencies 
    // are in Recaman series
    public static String isRecaman(String s)
    {
        // Store frequencies of characters
        Dictionary m = new Dictionary();
        for (int i = 0; i < s.Length; i++)
        {
            if (m.ContainsKey(s[i]))
                m[s[i]]++;
            else
                m.Add(s[i], 1);
        }
  
        // Get the size of the map
        int n = m.Count;
        int[] arr = new int[n + 1];
        recaman(arr, n);
        int flag = 1;
          
        // Compare vector elements with values in Map
        foreach (KeyValuePair mapEle in m)
        {
            int found = 0;
            for (int j = 1; j <= n; j++)
            {
                if (mapEle.Value == arr[j])
                {
                    found = 1;
                    break;
                }
            }
  
            if (found == 0)
            {
                flag = 0;
                break;
            }
        }
  
        if (flag == 1)
            return "YES";
        else
            return "NO";
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        String s = "geeekkkkkkss";
        Console.WriteLine(isRecaman(s));
    }
}
  
// This code is contributed by
// sanjeev2552


输出:
YES