| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: seen = set() last_non_empty = s for char in s: if char not in seen: seen.add(char) s = s.replace(char, '', 1) if s: last_non_empty = s else: break return last_non_empty ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| deepseek-r1 | ```python from collections import deque class Solution: def lastNonEmptyString(self, s: str) -> str: # Initialize deques for each character to track their occurrence positions deques = {chr(ord('a') + i): deque() for i in range(26)} for idx, c in enumerate(s): deques[c].append(idx) layers = [0] * len(s) current_layer = 0 max_layer = 0 while True: current_layer += 1 has_elements = False for c in 'abcdefghijklmnopqrstuvwxyz': if deques[c]: has_elements = True pos = deques[c].popleft() layers[pos] = current_layer max_layer = current_layer # Update max_layer each time a position is processed if not has_elements: break # If all layers are zero (unlikely as per problem constraints) if max_layer == 0: return "" # Collect characters in their original order where layer is max_layer result = [] for i in range(len(s)): if layers[i] == max_layer: result.append(s[i]) return ''.join(result) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| gpto1 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: n = len(s) counts = [0] * 26 # Counts of each character 'a' to 'z' iterations = [0] * n # Iteration at which each character is removed T = 0 # Total number of iterations for i in range(n): idx = ord(s[i]) - ord('a') counts[idx] += 1 iterations[i] = counts[idx] if counts[idx] > T: T = counts[idx] # The last iteration is T, so T - 1 is the iteration before the last result = [] for i in range(n): if iterations[i] > T - 1: result.append(s[i]) return ''.join(result) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| gpt-4o-2024-08-06 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: # We will keep track of the last non-empty string last_non_empty = s # We will use a list to allow for efficient character removal s_list = list(s) # Continue until the string becomes empty while s_list: # Mark the current state as the last non-empty string last_non_empty = ''.join(s_list) # Iterate over each character from 'a' to 'z' for char in range(ord('a'), ord('z') + 1): char = chr(char) # Try to remove the first occurrence of the character if char in s_list: s_list.remove(char) return last_non_empty ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| DeepSeek-Coder-V2-Lite-Instruct | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: while s: for char in range(ord('a'), ord('z') + 1): char = chr(char) if char in s: s = s.replace(char, '', 1) if not s: break return s ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: last_occurrence = {chr(i): -1 for i in range(ord('a'), ord('z') + 1)} for i in range(len(s)): last_occurrence[s[i]] = i max_index = -1 for i in range(len(s)): max_index = max(max_index, last_occurrence[s[i]]) if max_index == i: return s[i:] return "" ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: last_non_empty = "" while s: last_non_empty = s s = ''.join([c for c in s if c not in set(s)]) return last_non_empty ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| Qwen2-72B-Instruct | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: cnt, last = Counter(s), [] for c in reversed(s): if cnt[c] > 1: last.append(c) cnt[c] -= 1 return ''.join(last[::-1]) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| baichuan4 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: stack = [] for char in s: if char not in stack: stack.append(char) return ''.join(stack) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| doubao | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: alphabet = 'abcdefghijklmnopqrstuvwxyz' while s: for char in alphabet: if char in s: s = s.replace(char, '', 1) return s ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| gpto1 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: n = len(s) counts = [0] * 26 # Counts of each character 'a' to 'z' iterations = [0] * n # Iteration at which each character is removed T = 0 # Total number of iterations for i in range(n): idx = ord(s[i]) - ord('a') counts[idx] += 1 iterations[i] = counts[idx] if counts[idx] > T: T = counts[idx] # The last iteration is T, so T - 1 is the iteration before the last result = [] for i in range(n): if iterations[i] > T - 1: result.append(s[i]) return ''.join(result) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| kimi | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: char_index = {} for i, char in enumerate(s): if char not in char_index: char_index[char] = i max_index = max(char_index.values()) return s[:max_index] ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| minimax | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: # Create a set to keep track of seen characters seen = set() # Iterate over the string in reverse order for char in reversed(s): # If the character has not been seen, add it to the result if char not in seen: seen.add(char) else: # If the character has been seen, it will be removed # from the string in the next iteration, so we break break # Return the result string return ''.join(seen) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| wenxinyiyan | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: # Initialize a set to keep track of seen characters seen = set() # Initialize a stack to store the characters stack = [] # Iterate through the string for char in s: # If the character is not in the seen set, add it to the stack and the set if char not in seen: stack.append(char) seen.add(char) # Pop the last character from the stack (this will be the first character of the last non-empty string) last_char = stack.pop() # Initialize the result string with the last character result = last_char # Iterate through the remaining characters in the stack in reverse order for char in reversed(stack): # If the character is not in the seen set (after removing the last_char), # it means it belongs to the last non-empty string if char not in seen: result = char + result seen.remove(last_char) last_char = char else: # If the character is in the seen set, it means it was part of a previous non-empty string # so we can stop the iteration break return result ``` **Note**: The provided solution has a time complexity of O(n) where n is the length of the string `s`. However, due to the nature of the problem and the use of a stack, the space complexity is also O(n) in the worst case where all characters are unique. **Update**: After a closer look at the problem, it seems like we only need to keep track of the last occurrence of each character. We can optimize the space complexity by using a dictionary instead of a stack. Here's an updated solution: ```python class Solution: def lastNonEmptyString(self, s: str) -> str: # Initialize a dictionary to store the last index of each character last_index = {} # Iterate through the string and update the last index for each character for i, char in enumerate(s): last_index[char] = i # Sort the characters based on their last index in descending order sorted_chars = sorted(last_index.keys(), key=lambda x: last_index[x], reverse=True) # Initialize the result string result = "" # Iterate through the sorted characters for char in sorted_chars: # Add the character to the result string result += char # Remove the character from the last_index dictionary del last_index[char] # If the result string has the same length as the number of unique characters seen so far, # it means we have found the last non-empty string if len(result) == len(last_index) + 1: break # Reverse the result string since we built it in reverse order result = result[::-1] return result ``` This updated solution has a time complexity of O(nlogn) due to the sorting operation, where n is the number of unique characters in the string `s`. The space complexity is O(n) where n is the number of unique characters. |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| zhipu | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: # Dictionary to keep track of the last occurrence of each character last_occurrence = {} # Iterate over the string to find the last occurrence of each character for index, char in enumerate(s): last_occurrence[char] = index # Find the minimum index among the last occurrences of each character min_last_index = min(last_occurrence.values()) # Build the result string using characters up to the minimum last index result = [] for char in s: if char in last_occurrence and last_occurrence[char] <= min_last_index: result.append(char) # Update the min_last_index to ensure we only take the first occurrence min_last_index = min(min_last_index, last_occurrence[char] - 1) return ''.join(result) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |
| deepseek-v3 | ```python class Solution: def lastNonEmptyString(self, s: str) -> str: from collections import defaultdict, deque # Record the last occurrence of each character last_occurrence = {} for idx, char in enumerate(s): last_occurrence[char] = idx # Find the maximum last occurrence index max_last = max(last_occurrence.values()) # Collect characters that have their last occurrence at max_last result = [] for idx, char in enumerate(s): if last_occurrence[char] == max_last: result.append(char) return ''.join(result) ``` |
[{"input": "\"aabcbbca\"", "output": "\"ba\"", "testtype": "functional"}, {"input": "\"abcd\"", "output": "\"abcd\"", "testtype": "functional"}] |