当前位置:网站首页>Leetcode 115. different subsequences
Leetcode 115. different subsequences
2022-07-25 06:46:00 【Zero if】
Leetcode 115. Different subsequences
Topic linking
subject : Given a string s And a string t , Calculated at s In the subsequence of t Number of occurrences .
One of the strings Subsequence Refer to , By deleting some ( It can also be done without deleting ) Character and does not interfere with the relative position of the remaining characters of the new string .( for example ,“ACE” yes “ABCDE” A subsequence of , and “AEC” No )
The data of the questions ensure that the answers are in line with 32 Bit signed integer range .
Insert a code chip here
Input :s = "rabbbit", t = "rabbit"
Output :3
explain :
As shown in the figure below , Yes 3 Species can be obtained from s Get in "rabbit" The plan .
( Up arrow sign ^ Indicates the selected letter )
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
The solution is on the official website , Here is just a record of personal problems
Q1: Why is it dp[0][0] Time is the answer ?
A1: There is a key sentence in the solution :
Create a 2D array dp, among dp[i][j] It means that s[i:] In the subsequence of t[j:] Number of occurrences .
In the above expression ,s[i:] Express s From the subscript ii Substring to the end ,t[j:] Express t From the subscript j Substring to the end .
So when i=0,j=0 when , It means from s First character s[0] To end character s[m-1] In the subsequence of t[0] To t[n-1]( String character t) Number of occurrences .
Q2: Why? s[i] And t[j] Consider two aspects when equal ?s[i]=t[j] But what does mismatch mean ?
A2: When equal , You need to judge each t Whether the characters in are equal to s The characters in , When there is no match ,dp[i][j] = dp[i+1][j] It means if it doesn't match ,t[j] remain unchanged , Move s[i], This is the same as that in the title Subsequence Refer to , By deleting some ( It can also be done without deleting ) Character and does not interfere with the relative position of the remaining characters of the new string . Corresponding .
Finally, you can write questions , Don't forget to turn up the array , Or you'll cross the line .
Not necessarily right , If there are mistakes, you are welcome to point them out !
边栏推荐
- 2022 "strong country Cup" preliminary WP (with script and detailed process)
- How to troubleshoot the problem of too many inodes
- [C language] program environment and preprocessing
- 在C# WinForms应用程序中安装,配置和使用MetroFramework
- Over adapter mode
- 杜教筛
- 大话西游服务端启动注意事项
- 【每日一题】剑指 Offer II 115. 重建序列
- In container multicast
- [sword finger offer] analog implementation ATOI
猜你喜欢
![[Yugong series] July 2022 go teaching course 016 logical operators and other operators of operators](/img/36/9ad3f76078153f6af6c5b59d99564a.png)
[Yugong series] July 2022 go teaching course 016 logical operators and other operators of operators

分层强化学习综述:Hierarchical reinforcement learning: A comprehensive survey

EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移

Leetcode46 Full Permutation (Introduction to backtracking)
Shell script realizes the scheduled backup of MySQL database on two computers

微生物健康,不要排斥人体内微生物

JZ7 重建二叉树

Kyligence Li Dong: from the data lake to the index middle stage, improve the ROI of data analysis

使用 Web API 上传和下载多个文件

【愚公系列】2022年7月 Go教学课程 016-运算符之逻辑运算符和其他运算符
随机推荐
JZ7 rebuild binary tree
Prevention strategy of Chang'an chain Shuanghua transaction
C control open source library: download of metroframework
2022 Shenzhen cup
Learning notes: detailed use of 12864 LCD module
Use of golang exec.command
MySQL queries the table name under the current database
[datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization
Restrict Su command and sudo mechanism to promote nmap and console command netstat
Keilc51 usage details (III)
51 timer initial value calculation
Baidu xirang's first yuan universe auction ended, and Chen Danqing's six printmaking works were all sold!
不只是日志收集,项目监控工具Sentry的安装、配置、使用
JZ7 重建二叉树
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
[yolov5 practice 3] traffic sign recognition system based on yolov5 - model training
Analysis of the calling principle of Changan chain solid smart contract
Pic16f877xa instruction system (assembly language)
Save the sqoop run template
Software engineering in Code: regular expression ten step clearance