solution by python


  • 0
    L

    code:

    def isMatch(s,p):
        def unitmatch(string,reg):
            if len(string) != len(reg):
                return False
            else:
                for i in range(len(string)):
                    if string[i] != reg[i] and reg[i] != '.':
                        return False
                return True
        def k_list(n,k):
            if k == 1:
                return [[n]]
            elif n == 0:
                return [[0]*k]
            else:
                tmp_k_list = []
                for i in range(n+1):
                    for j in k_list(n-i,k-1):
                        tmp_k_list.append([i]+j)
                return tmp_k_list
        
        p_list = list(p)
        while p_list[0] == '*':
            p_list.pop(0)
        p_list_check = [p_list[0]]
        for i in range(1,len(p_list)):
            if not((p_list[i] == '*') and (p_list[i-1] == '*')):
                p_list_check.append(p_list[i])
        
        # calculation n,k
        k_number = 0
        for i in p_list_check:
            if i == '*':
                k_number += 1
        p_number = len(p_list_check) - 2 * k_number
    
        s_number = len(s)
        
        if p_number != s_number and k_number == 0:
            return False
        if p_number > s_number:
            return False
        n = s_number - p_number
        parameter_list = k_list(n,k_number)
        for i in parameter_list:
            p_change = []
            seeknum = 0
            for j in p_list_check:
                if j == '*':
                    parameter = p_change.pop(-1)
                    p_change.append(parameter*i[seeknum])
                    seeknum += 1
                else:
                    p_change.append(j)
            
            if unitmatch(s,''.join(p_change)):
                return True
        
        return False
    

Log in to reply
 

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