Check if frequency of characters are in Recaman Series
Given a string of lowercase alphabets. The task is to check whether the frequency of alphabets in this string, after arranging in any possible manner, forms the Recaman’s Sequence(excluding the first term).
Print “YES” if they are in sequence, otherwise output “NO”.
Few starting terms of Recaman’s Sequence are:
0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8 ….
Note: First term of Recaman’s Sequence is not considered since it is zero.
Examples:
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
Approach:
- Traverse the string and store the frequency of the characters in a map. Let the size of the map be N after storing the frequency.
- Now, make an array and insert first N elements of Recaman’s sequence in it.
- Now, traverse the array and check if the elements of the array are present as a key in map (excluding the check for zero).
- If each and every element of array is present in map, output “YES”, otherwise “NO”.
Below is the implementation of the above approach:
C++
// C++ program to check whether frequency of // characters in a string makes // Recaman Sequence #include <bits/stdc++.h> 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<char, int> 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 <Character, Integer> 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<char, int> m = new Dictionary<char, int>(); 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<char, int> 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
Recommended Posts:
- Program to check if all characters have even frequency
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Print characters in decreasing order of frequency
- String with frequency of characters in Lucas Sequence
- 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
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check if the Xor of the frequency of all digits of a number N is zero or not
- Check if the frequency of any character is more than half the length of the string
- Check whether the frequencies of all the characters in a string are prime or not
- Check if characters of one string can be swapped to form other
- Check whether count of distinct characters in a string is Prime or not
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Minimum number of operations to move all uppercase characters before all lower case characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
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.



