Repetitive String Matcher
In user-generated content platforms like Sharechat, users often express themselves with creative variations in text, such as repeating characters for emphasis (e.g., "cooool" instead of "cool"). To provide robust search, content moderation, or trend analysis, it's crucial to identify when two strings are fundamentally similar despite these superficial repetitions. This problem challenges you to implement a core component for such a system.
Implement a function isRepetitiveMatch(word: string, pattern: string) that determines if a given word repetitively matches a pattern string according to two specific rules. The function should return true if word matches pattern, and false otherwise.
Rules for Repetitive Matching:
word and pattern must normalize to the exact same string. The normalized form is obtained by reducing all sequences of consecutive identical characters to a single instance (e.g., "heeelloo" normalizes to "helo", "coool" normalizes to "col").word must be greater than or equal to the count of the same character in the pattern.word: A string representing the input word to be checked.pattern: A string representing the pattern to match against.boolean - true if word repetitively matches pattern based on the given rules, false otherwise.Examples
Example 1
Input:
word = "heeelloo", pattern = "helo"
Output:
true
Note:
Normalized forms for both are "helo". 'h': word (1) >= pattern (1). OK. 'e': word (3) >= pattern (1). OK. 'l': word (2) >= pattern (1). OK. 'o': word (2) >= pattern (1). OK.
Example 2
Input:
word = "ab", pattern = "aab"
Output:
false
Note:
Normalized forms for both are "ab". 'a': word (1) >= pattern (2) is FALSE. Thus, it's not a match.
Example 3
Input:
word = "apple", pattern = "aple"
Output:
true
Note:
Normalized forms for both are "aple". 'a': word (1) >= pattern (1). OK. 'p': word (2) >= pattern (1). OK. 'l': word (1) >= pattern (1). OK. 'e': word (1) >= pattern (1). OK.
Example 4
Input:
word = "", pattern = ""
Output:
true
Note:
Both are empty strings, which normalize to empty and satisfy the count condition vacuously.
Constraints
1 <= word.length, pattern.length <= 10^4 (for non-empty cases)word and pattern consist of lowercase English letters ('a'-'z').Loading editor...