You are given a string chatMessage. Your task is to count the number of substrings of exactly length 3 that contain at least one vowel (a, e, i, o, u, or their uppercase counterparts). Return this number as an integer.
Examples:
For chatMessage = "CodeSignal", the output should be solution(chatMessage) = 8.
The substrings of length 3 are: Cod, ode, deS, eSi, Sig, ign, gna, and nal. All these substrings contain at least one vowel. Thus, the answer is 8.
For chatMessage = "StrEngThen", the output should be solution(chatMessage) = 5.
The substrings of length 3 are: Str, trE, rEn, Eng, ngT, gTh, The, and hen. The substrings that contain at least one vowel are trE, rEn, Eng, The, and hen. Thus, the answer is 5.
Input/Output
[execution time limit] 4 seconds (py3)[memory limit] 1 GB[input] string chatMessage
A sequence of characters representing a chat message.[output] integer
The number of substrings of exactly length 3 where at least one character is a case-insensitive vowel.
这道 TikTok OA 题本质上是一个固定长度滑动窗口统计题:遍历字符串中所有长度恰好为 3 的连续子串,判断其中是否包含元音字母(大小写都算)。由于窗口长度固定为 3,不需要复杂的数据结构,直接用一次线性扫描即可;也可以维护窗口内元音数量,右端进入一个字符、左端移出一个字符,从而在 O(n) 时间内完成统计。题目给出的示例中,像 "CodeSignal" 会得到 8,而 "StrEngThen" 会得到 5。