classSolution:defpatternMatching(self,pattern:str,value:str) ->bool: n, m =len(value),len(pattern)if m ==0:returnTrueif n ==0elseFalse first, second ='a','b' first_num =sum([t == first for t in pattern]) second_num = m - first_numif second_num > first_num: first, second = second, first first_num, second_num = second_num, first_numif n ==0:returnTrueif second_num ==0elseFalse available = [] len_first, len_second =0,0while len_first <= n:if second_num ==0:if len_first >0: available.append((len_first, 0))else: len_second = (n - len_first * first_num) / second_numif len_second <1:breakif (n - len_first * first_num) % second_num ==0: available.append((len_first, int(len_second))) len_first +=1ifnot available:returnFalsefor flen, slen in available: f, s =None,None cur, bfalse =0,0for i, t1 inenumerate(pattern):if t1 == first: tmp = value[cur: cur + flen]if f isNone: f = tmpelse:if tmp != f: bfalse =1break cur += flenelse: tmp = value[cur: cur + slen]if s isNone: s = tmpelse:if tmp != s: bfalse =1break cur += slenif cur == n and i == m -1andnot bfalse:returnTruereturnFalse