| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 left = right + 1 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| deepseek-r1 | ``` curr = cnt[ord(word[left]) - ord('a')] valid -= 1 if curr == k else (-1 if curr == k+1 else 0) cnt[ord(word[left]) - ord('a')] -= 1 left += 1 ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| gpto1 | ```python while left < right + 1: curr = cnt[ord(word[left]) - ord('a')] valid -= 1 if curr == k else -1 if curr == k + 1 else 0 cnt[ord(word[left]) - ord('a')] -= 1 left += 1 ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| gpt-4o-2024-08-06 | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 cnt = [0]*26 valid = 0 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| DeepSeek-Coder-V2-Lite-Instruct | ```python class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| Qwen2-72B-Instruct | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 return result ``` ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| baichuan4 | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| doubao | ```python cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid -= 1 if cnt[ord(word[left-1])-ord('a')] == k else -1 if cnt[ord(word[left-1])-ord('a')] == k+1 else 0 } } } } return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| gpto1 | ```python while left < right + 1: curr = cnt[ord(word[left]) - ord('a')] valid -= 1 if curr == k else -1 if curr == k + 1 else 0 cnt[ord(word[left]) - ord('a')] -= 1 left += 1 ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| kimi | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| minimax | ```python cnt[ord(word[left])-ord('a')] -= 1 valid -= 1 if cnt[ord(word[left])-ord('a')] == k else -1 if cnt[ord(word[left])-ord('a')] == k+1 else 0 left += 1 ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| wenxinyiyan | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 cnt = [0]*26 return result ``` ### Explanation: The given starter code attempts to solve the problem using a sliding window approach, but it's incomplete. The missing portion handles the case where the difference between adjacent characters exceeds 2, which requires resetting the window and starting over. Here's a step-by-step explanation of the missing code: 1. After the inner loop (iterating over `right`) finishes processing a character, we check if the next character (if it exists) has a difference greater than 2 with the current character. This condition is given by `abs(ord(word[right+1])-ord(word[right])) > 2`. 2. If the condition is true, we need to reset the window because the substring we're currently considering cannot be extended further while satisfying the given constraints. 3. To reset the window, we need to: - Move the `left` pointer forward until it reaches the `right+1` position. This effectively discards all the characters in the current window. - Decrement the count of each discarded character in the `cnt` array. - Update the `valid` variable accordingly. If a character's count becomes `k` or `k+1` during this process, we need to adjust `valid` to reflect the change in the number of valid characters. 4. After resetting the window, we set `valid` to 0 and `cnt` to a new array of 26 zeros to prepare for the next substring. 5. Finally, we return the `result`, which represents the total number of complete substrings found. Note: The provided solution assumes that the input string `word` contains only lowercase English letters, as stated in the constraints. If the input can contain other characters, additional checks or modifications may be necessary. |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| zhipu | ```python curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 valid = 0 cnt = [0]*26 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |
| deepseek-v3 | ```python cnt[ord(word[left])-ord('a')] -= 1 left += 1 cnt = [0]*26 valid = 0 return result ``` |
class Solution(object): def countCompleteSubstrings(self, word, k): """ :type word: str :type k: int :rtype: int """ result = valid = 0 cnt = [0]*26 for c in range(1, len(set(word))+1): left = 0 for right in range(len(word)): cnt[ord(word[right])-ord('a')] += 1 curr = cnt[ord(word[right])-ord('a')] valid += 1 if curr == k else -1 if curr == k+1 else 0 if right-left+1 == c*k+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 if valid == c: result += 1 if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2: while left < right+1: curr = cnt[ord(word[left])-ord('a')] valid -= 1 if curr == k else -1 if curr == k+1 else 0 cnt[ord(word[left])-ord('a')] -= 1 left += 1 return result |