Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if frequency of characters are in Recaman Series in Python
Suppose we have a lowercase string s. We have to check whether the occurrences of alphabets in s, after rearranging in any possible manner, generates the Recaman's Sequence (ignoring the first term).
The Recaman's sequence is defined as follows:
- a? = 0
- a? = a??? - n (if a??? - n > 0 and not already present in sequence)
- a? = a??? + n (otherwise)
Some of the items of Recaman's Sequence are [0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24,...]. The first term (0) is ignored for this problem.
Example
If the input is s = "pppuvuuqquuu", then the output will be True. The character frequencies are: (p, 3), (u, 6), (v, 1) and (q, 2). The frequencies [1, 3, 6, 2] can be rearranged to match the first four terms of Recaman's Sequence [1, 3, 6, 2].
Algorithm
To solve this problem, we follow these steps ?
- Count frequency of each character in the string
- Generate the first n terms of Recaman's sequence (where n is the number of unique characters)
- Check if all character frequencies match the generated Recaman terms
- Return True if all frequencies are found, False otherwise
Implementation
from collections import defaultdict
def generate_recaman(n):
"""Generate first n+1 terms of Recaman's sequence"""
array = [0] * (n + 1)
array[0] = 0
for i in range(1, n + 1):
temp = array[i - 1] - i
# Check if temp is positive and not already in sequence
if temp > 0 and temp not in array[:i]:
array[i] = temp
else:
array[i] = array[i - 1] + i
return array
def check_recaman_frequency(s):
"""Check if character frequencies match Recaman sequence"""
# Count character frequencies
freq = defaultdict(int)
for char in s:
freq[char] += 1
n = len(freq)
# Generate first n terms of Recaman sequence (excluding 0)
recaman_terms = generate_recaman(n)
recaman_set = set(recaman_terms[1:]) # Exclude first term (0)
# Check if all frequencies exist in Recaman terms
for frequency in freq.values():
if frequency not in recaman_set:
return False
return True
# Test the function
s = "pppuvuuqquuu"
result = check_recaman_frequency(s)
print(f"String: {s}")
print(f"Result: {result}")
# Show the frequencies and Recaman terms for verification
freq = defaultdict(int)
for char in s:
freq[char] += 1
print(f"Character frequencies: {dict(freq)}")
recaman_terms = generate_recaman(len(freq))
print(f"Recaman sequence terms: {recaman_terms[1:]}") # Exclude 0
String: pppuvuuqquuu
Result: True
Character frequencies: {'p': 3, 'u': 6, 'v': 1, 'q': 2}
Recaman sequence terms: [1, 3, 6, 2]
How It Works
The algorithm works by:
- Frequency counting: Count how many times each character appears in the string
- Recaman generation: Generate the first n terms of Recaman's sequence (excluding 0)
- Matching: Check if each character frequency exists in the generated Recaman terms
For the example "pppuvuuqquuu", the frequencies are [3, 6, 1, 2] which all exist in the first 4 Recaman terms [1, 3, 6, 2].
Optimized Version
from collections import Counter
def is_recaman_frequency(s):
"""Optimized version using Counter and set operations"""
# Get character frequencies
frequencies = list(Counter(s).values())
n = len(frequencies)
# Generate Recaman sequence
recaman = [0]
for i in range(1, n + 1):
temp = recaman[i-1] - i
if temp > 0 and temp not in recaman:
recaman.append(temp)
else:
recaman.append(recaman[i-1] + i)
# Check if frequencies match Recaman terms (excluding 0)
return set(frequencies) <= set(recaman[1:])
# Test
test_string = "pppuvuuqquuu"
print(f"Input: {test_string}")
print(f"Is Recaman frequency: {is_recaman_frequency(test_string)}")
Input: pppuvuuqquuu Is Recaman frequency: True
Conclusion
This solution efficiently checks if character frequencies in a string can form terms of the Recaman sequence. The key insight is generating the appropriate number of Recaman terms and using set operations for fast lookup and comparison.
