| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python for i in KMP(s, a): for j in lookup: if abs(i - j) <= k: result.append(i) break return sorted(result) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| deepseek-r1 | ```python list_a = list(KMP(s, a)) import bisect for i in list_a: left = i - k right = i + k pos = bisect.bisect_left(lookup, left) if pos < len(lookup) and lookup[pos] <= right: result.append(i) return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| gpto1 | ```python import bisect indices_a = list(KMP(s, a)) result = [] for i in indices_a: left = bisect.bisect_left(lookup, i - k) right = bisect.bisect_right(lookup, i + k) if left < right: result.append(i) return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| gpt-4o-2024-08-06 | ```python for i in KMP(s, a): for j in lookup: if abs(j - i) <= k: result.append(i) break return sorted(result) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python for i in KMP(s, a): for j in lookup: if abs(i - j) <= k: result.append(i) break return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python for i in KMP(s, a): for j in lookup: if abs(i - j) <= k: result.append(i) break return sorted(set(result)) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| DeepSeek-Coder-V2-Lite-Instruct | ```python for i in range(len(s) - len(a) + 1): if s[i:i+len(a)] == a: for j in lookup: if abs(i - j) <= k: result.append(i) break return sorted(result) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| Qwen2-72B-Instruct | ```python for i in KMP(s, a): for j in lookup: if abs(i - j) <= k: result.append(i) break return sorted(list(set(result))) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| baichuan4 | ```python for i in range(len(s) - len(a) + 1): if s[i:i+len(a)] == a: for j in lookup: if abs(i - j) <= k: result.append(i) break return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| doubao | ```python for i in range(len(s) - len(a) + 1): if s[i:i + len(a)] == a: for j in lookup: if abs(i - j) <= k: result.append(i) break return sorted(result) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| gpto1 | ```python import bisect indices_a = list(KMP(s, a)) result = [] for i in indices_a: left = bisect.bisect_left(lookup, i - k) right = bisect.bisect_right(lookup, i + k) if left < right: result.append(i) return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| kimi | ```python for i in KMP(s, a): j = 0 while j < len(lookup) and abs(lookup[j] - i) > k: j += 1 if j < len(lookup): result.append(i) return sorted(result) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| minimax | ```python for i in range(len(a)): for j in range(len(s) - len(a) + 1): if s[j:j+len(a)] == a: for idx in lookup: if abs(j - idx) <= k: result.append(j) break return sorted(set(result)) ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| wenxinyiyan | ```python a_indices = list(KMP(s, a)) for idx in a_indices: for lookup_idx in lookup: if abs(lookup_idx - idx) <= k: result.append(idx) break return sorted(set(result)) ``` **Note**: The provided solution uses the Knuth-Morris-Pratt (KMP) algorithm to efficiently find all occurrences of substrings `a` and `b` within the string `s`. However, the solution has a potential efficiency issue due to the nested loop used to check the condition `|j - i| <= k` for each occurrence of `a`. This approach may not be optimal for very long strings or large values of `k`. To optimize the solution further, you could consider using additional data structures, such as interval trees or sorted sets, to efficiently query the existence of indices `j` within the range `[i - k, i + k]` for each occurrence `i` of substring `a`. However, implementing such data structures is beyond the scope of this simple completion task. The provided solution should work correctly for most cases and is sufficient to demonstrate the basic idea of solving the problem. |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| zhipu | ``` for i in KMP(s, a): left = bisect.bisect_left(lookup, i - k) right = bisect.bisect_right(lookup, i + k) if left < right: result.append(i) return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |
| deepseek-v3 | ```python a_indices = list(KMP(s, a)) if not lookup: return result for i in a_indices: low = max(0, i - k) high = min(len(s) - len(b), i + k) left = bisect.bisect_left(lookup, low) right = bisect.bisect_right(lookup, high) if left < right: result.append(i) return result ``` |
class Solution(object): def beautifulIndices(self, s, a, b, k): """ :type s: str :type a: str :type b: str :type k: int :rtype: List[int] """ def getPrefix(pattern): prefix = [-1]*len(pattern) j = -1 for i in range(1, len(pattern)): while j+1 > 0 and pattern[j+1] != pattern[i]: j = prefix[j] if pattern[j+1] == pattern[i]: j += 1 prefix[i] = j return prefix def KMP(text, pattern): prefix = getPrefix(pattern) j = -1 for i in range(len(text)): while j+1 > 0 and pattern[j+1] != text[i]: j = prefix[j] if pattern[j+1] == text[i]: j += 1 if j+1 == len(pattern): yield i-j j = prefix[j] result = [] if not (len(a) <= len(s) and len(b) <= len(s)): return result lookup = list(KMP(s, b)) j = 0 for i in KMP(s, a): while j < len(lookup) and lookup[j] < i-k: j += 1 if j < len(lookup) and lookup[j] <= i+k: result.append(i) return result |