You are given an array of strings words. For each index i in the range [0, words.length…
- User
You are given an array of strings words. For each index i in the range [0, words.length - 1], perform the following steps: Remove the element at index i from the words array. Compute the length of the longest common among all adjacent pairs in the modified array. Return an array answer, where answer[i] is the length of the longest common prefix between the adjacent pairs after removing the element at index i. If no adjacent pairs remain or if none share a common prefix, then answer[i] should be 0. Voici ma solution mais elle est lente: class Solution(object): def longestCommonPrefix(self, words): """ :type words: List[str] :rtype: List[int] """ solution = [0 for i in range(len(words))] already_compute = {} import os def longuest_prefix(w1, w2): return len(os.path.commonprefix([w1, w2])) for i in range(len(words)): remaining_words = [x for j,x in enumerate(words) if j != i] #print(remaining_words) if len(remaining_words) < 2: solution[i] = 0 pass for k in range(0, len(remaining_words)-1): #print(remaining_words[k]) l = already_compute.get((remaining_words[k],remaining_words[k+1]), -1) if l == -1: l = longuest_prefix(remaining_words[k],remaining_words[k+1]) already_compute[(remaining_words[k],remaining_words[k+1])] = l solution[i] = max(solution[i],l) return solution
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.