当前位置:网站首页>LeetCode: 392. Judgment Subsequence —— Simple
LeetCode: 392. Judgment Subsequence —— Simple
2022-08-06 15:02:00 【Kinght_123】

题目
392. 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串.(例如,"ace"是"abcde"的一个子序列,而"aec"不是).
进阶:
如果有大量输入的 S,称作 S1, S2, … , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列.在这种情况下,你会怎样改变代码?
示例 1:
输入:s = "abc", t = "ahbgdc"
输出:true
示例 2:
输入:s = "axc", t = "ahbgdc"
输出:false
提示:
- 0 <= s.length <= 100
- 0 <= t.length <= 10^4
- 两个字符串都只由小写字符组成.
解题思路1
- It's easy to think of a double-pointer approach.
Code
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
m, n = len(s), len(t)
i, j = 0, 0
while i < m and j < n:
if s[i] == t[j]:
i += 1
j += 1
return i == m
运行结果

解题思路2
- Problem solving through dynamic programming.
Code
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
n, m = len(s), len(t)
f = [[0] * 26 for _ in range(m)]
f.append([m] * 26)
for i in range(m - 1, -1, -1):
for j in range(26):
f[i][j] = i if ord(t[i]) == j + ord('a') else f[i + 1][j]
add = 0
for i in range(n):
if f[add][ord(s[i]) - ord('a')] == m:
return False
add = f[add][ord(s[i]) - ord('a')] + 1
return True
运行结果

边栏推荐
- 重新生成一堆rpm目录的repo库步骤
- 天梯赛真题——7-6 老板的作息表(25 分)
- car audio service详解
- CSDN 21-day Learning Challenge - The first punching article
- 【Paper Speed Reading】NLNL: Negative Learning for Noisy Labels (ICCV2019)
- mutex try_lock spin lock read-write lock atomic operation shared memory
- SAP BAPI 教程 – 在 ABAP 中创建 BAPI 的分步指南-020
- 挖财股票开户安全吗?怎么开账户是安全可靠的?
- PCs really don't work anymore!Intel and AMD suffer the same fate
- [2022CISCN]初赛 复现
猜你喜欢

SQL执行一次完成新增或者修改操作-2022新项目

New kernel PHP enterprise website development and construction management system

PCs really don't work anymore!Intel and AMD suffer the same fate

从技术全景到场景实战,透析「窄带高清」的演进突破

Unstoppable, a major breakthrough in China's chip manufacturing industry chain, 5nm equipment will soon be sent to TSMC

如何从一个空有上进心的人,变成行动上的巨人?

Practical+Reverse+Engineering Chapter 3 List Exercises

DAO:Web3 的必要组件

Intel transformation was not successful and into loss, AMD grow again and get substantial profits

LeetCode Brushing Diary: 899. Ordered Queue
随机推荐
Another proof of the strength of China's manufacturing, the number of Chinese companies in the top 500 is the largest
【组成原理 七 I/O系统】
”元宇宙“必须具备这些特点
Apache Calcite入门
兆骑科创双创服务平台,创新创业高层次人才引进,投融资对接
机器视觉需要学什么?学习机器视觉需要掌握哪些知识?
【直播预告】对话知道创宇丨如何守住内容安全生命线?
什么是元宇宙?
js array to remove the specified element [function encapsulation] (including object array to remove the specified element)
How much does Ark Survival Evolved self-built server cost?
【LeetCode】658.找到K个最接近的数
paper速读专栏索引
CSDN21天学习挑战赛 - 第一篇打卡文章
巴比特 | 元宇宙每日必读:什么是中国特色的元宇宙之路?边界和机会在哪里?...
科利转债上市价格预测
小程序跳转方式
[2022CISCN]初赛 复现
mysql8 新特性注入
一个案例搞懂工厂模式和单例模式
CSDN 21-day Learning Challenge - The first punching article