Powered by:NEFU AB-IN
97. 交错字符串
题意
给定三个字符串 s1
、s2
、s3
,请你帮忙验证 s3
是否是由 s1
和 s2
交错组成的。
两个字符串 s
和 t
交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
- 交错 是
s1 + t1 + s2 + t2 + s3 + t3 + ...
或者t1 + s1 + t2 + s2 + t3 + s3 + ...
注意:a + b
意味着字符串 a
和 b
连接。
思路
还是记忆化搜索
dp 四个状态
串a的下标 串b的下标 串c的下标 到串a还是串b了
对于这种如果存在True的结果,就是True的dfs,所有状态的递推采取or
代码
'''
Author: NEFU AB-IN
Date: 2024-09-03 15:36:27
FilePath: \LeetCode\97\97.py
LastEditTime: 2024-09-03 16:28:52
'''
import random
from collections import Counter, defaultdict, deque
from datetime import datetime, timedelta
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush, nlargest, nsmallest
from itertools import combinations, compress, permutations, starmap, tee
from math import ceil, comb, fabs, floor, gcd, hypot, log, perm, sqrt
# 3.8.9 import
from posixpath import abspath
from string import ascii_lowercase, ascii_uppercase