You are given a string s consisting of lowercase English letters and an integer k.
Your task is to construct a new string that contains only those characters from s which appear fewer than k times in the entire string. The order of characters in the new string must be the same as their order in s.
Return the resulting string. If no characters qualify, return an empty string.
Note: Every occurrence of a character that occurs fewer than k times is kept.
Example 1:
Input: s = "aadbbcccca", k = 3
Output: "dbb"
Explanation:
Character frequencies in s:
'a' appears 3 times'd' appears 1 time'b' appears 2 times'c' appears 4 timesOnly 'd' and 'b' appear fewer than 3 times. Preserving their order, the result is "dbb".
Example 2:
Input: s = "xyz", k = 2
Output: "xyz"
Explanation:
All characters ('x', 'y', 'z') appear exactly once, which is fewer than 2. Thus the whole string is returned.
Constraints:
1 <= s.length <= 100s consists of lowercase English letters.1 <= k <= s.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem requires filtering characters in a string based on their total occurrences. Characters that appear strictly fewer than a specified threshold are preserved in their original order. The solution computes character frequencies first and then filters the string accordingly.
Here's how the algorithm would work step-by-step:
from collections import Counter
def filter_characters_by_frequency(input_string, threshold):
# Count the frequency of each character across the input string
frequency_map = Counter(input_string)
result_characters = []
# Iterate through characters and keep those appearing fewer than the threshold
for current_char in input_string:
if frequency_map[current_char] < threshold:
result_characters.append(current_char)
# Combine the filtered characters into the final string
return "".join(result_characters)
This problem requires filtering characters from an input string based on their total occurrences. Characters with an overall frequency strictly less than a specified threshold are retained in their original sequence. The solution determines frequencies first and then performs a linear scan to construct the final output.
Here's how the algorithm would work step-by-step:
from collections import Counter
def filter_characters_by_frequency(input_string: str, threshold: int) -> str:
# Count the total occurrences of each character in the string
frequency_map = Counter(input_string)
filtered_characters = []
# Iterate through each character to keep those below the threshold frequency
for current_char in input_string:
if frequency_map[current_char] < threshold:
filtered_characters.append(current_char)
# Combine the remaining characters into a single string
return "".join(filtered_characters)
| Case | How to Handle |
|---|---|
| Empty string input | Return the empty string immediately since no characters meet the frequency threshold. |
| K is greater than the length of the string | Return an empty string because no character can possibly appear k times. |
| K is equal to zero or negative | Treat all characters as valid since every character appears at least zero or negative times. |
| All characters appear fewer than k times | Filter out all characters and return an empty string. |
| All characters appear at least k times | Preserve the original string entirely and return it as is. |
| String contains non-alphanumeric characters, spaces, or unicode symbols | Count and filter them using their exact character values without special handling. |
| Extremely long strings reaching maximum memory limits | Use a linear pass frequency map and a second filtering pass to operate efficiently in O(n) time. |