Solution checker results in runtime error

  • 0

    Submitted solution that accommodates the ridiculous definition of prefix and now it comes up with runtime error with too many inputs to unpack.

    These definitions of prefix are contradictory and special case. I made the exception to return the first string if list is all exact duplicates but now its all runtime error for too many inputs to unpack.
    ['a'] == 'a' but ['','b'] == ''
    ['c','c'] == 'c' but ['a','a','b'] == ''

    Someone please unclog the solution checker or delete this problem. Currently its teaching bad programming solutions.

  • 0

    The test cases input should be string[], like ["c","cv","ccc"]. However, your inputs are all char[].
    There are totally two different types. Change your input and see what happens then before complaining how bad the checker it is.

  • 0

    I think you misunderstand. These are not test cases I made up, but test cases that I discovered was in the checker through trial and error. I understand the inputs to the tester are string. In some languages a single character is a char. My code is written and checked in python where string is immutable and char is not an accessible datatype of the language and all characters found between quotations are strings regardless of character length.

    class Trie(object):
        def __init__(self):
            self.root_node = {}
            self.singleString = False
        def check_and_add_new(self, str):
            rlength = 0
            length = 0
            rprefix = ''
            prefix = ''
            trie = self.root_node
            isPrefix = False
            for idx, c in enumerate(str):
                if c in trie:
                    if idx == 0:
                        isPrefix = True
                    if isPrefix:
                        length += 1
                        prefix = prefix + c
                    if self.singleString:
                        rlength += 1
                        rprefix = rprefix + c
                    trie[c] = {}
                    if isPrefix:
                        rlength = length
                        rprefix = prefix
                    if self.singleString:
                        self.singleString = False
                        rlength = 100000
                        rprefix = ''
                trie = trie[c]
            # First Pass
            if len(self.root_node.keys()) == 1:
                self.singleString = True
                rprefix = prefix
            return rlength, rprefix, self.singleString
    class Solution(object):
        def longestCommonPrefix(self, strs):
            :type strs: List[str]
            :rtype: str
            #Super Trie: < n memory, n*len(str) speed
            lengthLongest = 0
            longestPrefix = ''
            tree = Trie()
            for str in strs:
                length, prefix = tree.check_and_add_new(str)
                if length > lengthLongest:
                    longestPrefix = prefix
                    lengthLongest = length
            return longestPrefix

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.