String Transformation with Character Counts
This challenge asks you to implement a function that transforms a string based on a provided character count map. The function should replace each character in the input string with a specified replacement character if the character's count in the map matches a given target count. This is useful for data masking, redaction, or applying specific transformations based on character frequency.
Problem Description
You are tasked with creating a TypeScript function called transformString that takes three arguments:
inputString: A string to be transformed.charCounts: An object where keys are characters and values are their counts within theinputString.targetCount: A number representing the count to match for character replacement.
The function should iterate through the inputString. For each character, it should check if the character exists as a key in the charCounts object and if its corresponding value (count) is equal to the targetCount. If both conditions are true, the character should be replaced with the character 'X'. Otherwise, the original character should be retained.
The function should return the transformed string.
Key Requirements:
- The function must be written in TypeScript.
- The function must handle empty input strings gracefully.
- The function must correctly identify and replace characters based on the provided
charCountsandtargetCount. - The function should not modify the original
inputString. - The function should be efficient in its processing.
Expected Behavior:
The function should return a new string where characters matching the specified count are replaced with 'X'.
Edge Cases to Consider:
- Empty
inputString. charCountsobject being empty.targetCountbeing zero or negative.- Characters in
inputStringnot present incharCounts. charCountscontaining characters not present ininputString.
Examples
Example 1:
Input: inputString = "hello world", charCounts = { l: 3, o: 2 }, targetCount = 3
Output: "heXlo worXd"
Explanation: The character 'l' appears 3 times in "hello world", which matches the targetCount of 3. Therefore, all 'l's are replaced with 'X'. The character 'o' appears 2 times, which does not match the targetCount of 3, so it remains unchanged.
Example 2:
Input: inputString = "aabbcc", charCounts = { a: 2, b: 2, c: 2 }, targetCount = 2
Output: "XXXX"
Explanation: All characters 'a', 'b', and 'c' appear 2 times, matching the targetCount. Therefore, all characters are replaced with 'X'.
Example 3:
Input: inputString = "abc", charCounts = { a: 1, b: 1, c: 1 }, targetCount = 4
Output: "abc"
Explanation: None of the characters 'a', 'b', or 'c' have a count of 4. Therefore, the string remains unchanged.
Example 4:
Input: inputString = "", charCounts = { a: 1 }, targetCount = 1
Output: ""
Explanation: The input string is empty, so the function returns an empty string.
Constraints
inputStringwill contain only ASCII characters.charCountswill be an object with string keys (characters) and number values (counts).targetCountwill be a non-negative integer.- The length of
inputStringwill be between 0 and 1000 characters (inclusive). - The number of unique characters in
charCountswill be between 0 and 26 (inclusive). - Performance: The function should complete within 100ms for all valid inputs.
Notes
Consider using a for...of loop to iterate through the inputString. Building a new string character by character is generally more efficient than repeatedly modifying an existing string. Remember to create a new string to return, rather than modifying the original. Think about how to handle cases where a character is not found in charCounts.