CoffeeScript implementation since it beats 100% JS


  • 0
    A

    Basically, reprint from https://discuss.leetcode.com/topic/6617/an-o-n-solution-with-detailed-explanation

    # findSubstring :: String -> [String] -> [Int]
    findSubstring = (s, words) ->
      single_length = words[0].length
      total_length = single_length * words.length
      ret = []
      goal_o = {}
      for w in words
        if goal_o[w]
          goal_o[w]++
        else
          goal_o[w] = 1
      for i in [0..single_length-1] by 1
        tmp = {}
        j = i
        while true
          break if j + single_length > s.length
          w = s[j...j+single_length]
          if goal_o[w] is undefined
            while i != j
              w = s[i...i+single_length]
              tmp[w]--
              i += single_length
            i += single_length
            j += single_length
            continue
          if tmp[w] is undefined
            tmp[w] = 1
          else
            if tmp[w] == goal_o[w]
              while true
                to_delete = s[i...i+single_length]
                i += single_length
                break if to_delete is w
                tmp[to_delete]--
            else
              tmp[w]++
          j += single_length
          if j == i + total_length
            ret.push i
      ret
    

Log in to reply
 

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