# String with frequency of characters in Lucas Sequence

Given a string ‘str’ containing lowercase English alphabets, the task is to find whether the frequencies of the characters of the string are in Lucas sequence or not. You are free to arrange the frequency numbers in any way, in order to form the Lucas sequence. If possible then print *YES *otherwise *NO*.

**Note:** All the frequencies must be used to check if they are in Lucas Sequence.

**Examples:**

Input:str = “gggeek”

Output:YES

frequency of ‘g’ = 3

frequency of ‘e’ = 2

frequency of ‘k’ = 1

These frequency can be arranged to form first 3 terms of Lucas sequence, {2, 1, 3}.

Input:str = “geeksforgeeks”

Output:NO

**Approach:**

- Store the frequency of each character in a vector using map STL. Sort the vector afterwards.
- Change the first and second element of first vector to ‘2’ and ‘1’ respectively, since Lucas sequence has ‘2’ and ‘1’ as first two numbers. However, change must only be made if ‘1’ and ‘2’ are present in the vector. If it is not present, then the frequencies can never be in Lucas sequence and output NO.
- Then, make another vector. Let the size of first vector be n.
- Insert first ‘n’ Lucas numbers in the 2nd vector.
- Then, compare each element in both vectors. If both vectors are same, output ‘YES’, otherwise output ‘NO’.

Below is the implementation of the above approach:

## C++

`// C++ implementation of ` `// the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function that checks ` `// if the frequencies ` `// are in Lucas sequence. ` `string lucas_sequence(string s, ` `int` `n) ` `{ ` ` ` `// map is used to store ` ` ` `// character frequencies ` ` ` `map<` `char` `, ` `int` `> m; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(m.find(s[i]) == m.end()) ` ` ` `m[s[i]] = 1; ` ` ` `else` ` ` `m[s[i]]++; ` ` ` `} ` ` ` ` ` `vector<` `int` `> v1, v2; ` ` ` ` ` `map<` `char` `, ` `int` `>::iterator it; ` ` ` ` ` `// frequencies are extracted from ` ` ` `// map and stored in vector v1 ` ` ` `for` `(it = m.begin(); it != m.end(); it++) ` ` ` `v1.push_back((*it).second); ` ` ` ` ` `// vector v1 elements are sorted, ` ` ` `// but first and second element are ` ` ` `// changed to '2' and '1' respectively, ` ` ` `// only if '1' and '2' are present in the vector. ` ` ` `sort(v1.begin(), v1.end()); ` ` ` `if` `(v1[0] == 1 && v1[1] == 2) { ` ` ` `v1[0] = 2; ` ` ` `v1[1] = 1; ` ` ` `} ` ` ` `else` ` ` `return` `"NO"` `; ` ` ` ` ` `// a and b are first and ` ` ` `// second terms of ` ` ` `// Lucas sequence ` ` ` `int` `a = 2, b = 1; ` ` ` `int` `c; ` ` ` `v2.push_back(a); ` ` ` `v2.push_back(b); ` ` ` ` ` `// v2 contains Lucas sequence ` ` ` `for` `(` `int` `i = 0; i < v1.size() - 2; i++) { ` ` ` `v2.push_back(a + b); ` ` ` `c = a + b; ` ` ` `a = b; ` ` ` `b = c; ` ` ` `} ` ` ` `int` `flag = 1; ` ` ` ` ` `// both vectors are compared ` ` ` `for` `(` `int` `i = 0; i < v1.size(); i++) { ` ` ` `if` `(v1[i] != v2[i]) { ` ` ` `flag = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(flag == 1) ` ` ` `return` `"YES"` `; ` ` ` `else` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"oooeeeeqkk"` `; ` ` ` `int` `n = s.length(); ` ` ` `cout << lucas_sequence(s, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

from collections import defaultdict

# Function that checks if the

# frequencies are in Lucas sequence.

def lucas_sequence(s, n):

# map is used to store

# character frequencies

m = defaultdict(lambda:0)

for i in range(0, n):

m[s[i]] += 1

v1, v2 = [], []

# frequencies are extracted from

# map and stored in vector v1

for it in m:

v1.append(m[it])

# vector v1 elements are sorted, but

# first and second element are changed

# to ‘2’ and ‘1’ respectively, only if

# ‘1’ and ‘2’ are present in the vector.

v1.sort()

if v1[0] == 1 and v1[1] == 2:

v1[0], v1[1] = 2, 1

else:

return “NO”

# a and b are first and second terms

# of Lucas sequence

a, b = 2, 1

v2.append(a)

v2.append(b)

# v2 contains Lucas sequence

for i in range(0, len(v1) – 2):

v2.append(a + b)

a, b = b, a + b

flag = 1

# both vectors are compared

for i in range(0, len(v1)):

if v1[i] != v2[i]:

flag = 0

break

if flag == 1:

return “YES”

else:

return “NO”

# Driver code

if __name__ == “__main__”:

s = “oooeeeeqkk”

n = len(s)

print(lucas_sequence(s, n))

# This code is contributed by Rituraj Jain

**Output:**

YES

## Recommended Posts:

- Check whether frequency of characters in a string makes Fibonacci Sequence
- Remove even frequency characters from the 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
- Longest sub-sequence of array containing Lucas numbers
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Program to check if all characters have even frequency
- Check if frequency of all characters can become same by one removal
- Check if frequency of characters are in Recaman Series
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Frequency of a substring in a string
- Frequency of a string in an array of strings
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find frequency of each word in a string in Python
- Longest sub-string having frequency of each character less than equal to k

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.