Python program for checking Concatenation of All words in String

When a words list is given and string is s.

Find the starting indices of all concatenated strings in s.

For example:
s : ababab
words = [ "ab" , "ab" , "ab" ] so concatenation "ababab" matches at index 0
incides = [ 0 ]

This is famous leetcode 30

from itertools import permutations
import re 

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        if not s or not words : 
            return []
        # the brute force way , timed out 
        """
        indices = [] 
        all_pers = permutations(words) ; 
        for per in all_pers:
            match = f"(?={"".join(list(per))})"
            index = [m.start() for m in re.finditer( match , s  ) ] ;
            #index = s.find("".join(list(per)))
            if index != [] :
                indices.extend(index)
        indices = list(set(indices))
        indices.sort()
        return indices
        """
        '''
        # failed time limit 
        # this approach was using frequency, it works 
        # new hmm
        #s =  "abaababbaba";  words = ["ab","ba","ab","ba"]
        #all_chars = "".join(words)
        #possible_chars = [ words[i][0] + words[j][-1]  for i in range(0 , len(words)) for j in range(0,len(words))  if i != j  ] ;
        #if len(words) == 1:
        #    possible_chars = [words[0][0] + words[0][-1] ] ; print(possible_chars) ;
        #words_ch_f = { wch : all_chars.count(wch) for wch in all_chars }
        words_f = { w : words.count(w) for w in words } ; indices = [] # hihi :)
        cl = sum([ len(w) for w in words ]) # this can be done simply no of words * len of words[0]
        # this is concatenated string 
        # formula
        #xs = "cdefab" ; 
        #xs_f = { w : xs.count("cd") for w in words_f };
        ln = len(s) 
        for i in range(len(s)):
            # print string
            chunk_str = s[i:i+cl]
            #print(chunk_str)
            if len(s) - i >= cl  :
                xs_f = { w : 0  for w in words_f } ; w = words[0]
                fl = True
                for j in range(0 , len(chunk_str) , len(w) ):
                    word = chunk_str[j:j+len(w)]
                    if word not in xs_f:
                        fl = False
                        break
                    xs_f[word] += 1
                if fl == False:
                    continue
                if xs_f == words_f :
                    indices.append(i)
                """
                # check for char freq
                xs_char_f = { wch : chunk_str.count(wch) for wch in words_ch_f }; print(words_ch_f)
                fl_char = True
                for k , v in words_ch_f.items():
                    if xs_char_f[k] < v :
                        fl_char = False
                print("Char fre passed" if fl_char else "Char fre failed!")
                # first and last char same
                char_valid = False
                for pc in possible_chars :
                    if pc[0] == chunk_str[0] and pc[-1] == chunk_str[-1] :
                        char_valid = True
                        break
                print("Char 0 and -1 valid " if char_valid else "Char not valid ! ")
                """
        # new umm



        return indices


        








        









Output:



Comments