[LeetCode]Distinct Subsequences,解题报告
题目
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
思路1
import java.util.Scanner;public class DistinctSubsequences {private static int disNum = 0;public static int numDistinct(String S, String T) {int[] flags = new int[S.length()];int num = 0;dfs(num, flags, 0, 0, S, T);return disNum;}public static void dfs(int num, int[] flags, int indexS, int indexT, String S, String T) {if (num == T.length()) {disNum++;} else {for (int i = indexS; i < S.length(); i ++) { if (S.charAt(i) == T.charAt(indexT) && flags[i] == 0) {flags[i] = 1;num++;dfs(num, flags, i + 1, indexT + 1, S, T);flags[i] = 0;num--;}}}}public static void main(String[] args) {Scanner cin = new Scanner(System.in);while (cin.hasNext()) {String S = cin.nextLine();String T = cin.nextLine();disNum = 0;int res = numDistinct(S, T);System.out.println(res);}cin.close();}
}
可是在大集合的时候 Time Limit Exceeded
思路2
能够參考动态规划经典的样例。最长公共子序列。
public class Solution {public static int numDistinct(String S, String T) {if (S == null || S.length() == 0) {return 0;}int[][] dp = new int[T.length() + 1][S.length() + 1];dp[0][0] = 1;for (int i = 1; i <= S.length(); i++) {dp[0][i] = 1;}for (int i = 1; i <= T.length(); i++) {dp[i][0] = 0;}for (int i = 1; i <= T.length(); i++) {for (int j = 1; j <= S.length(); j++) {if (T.charAt(i - 1) == S.charAt(j - 1)) {dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];} else {dp[i][j] = dp[i][j - 1];}}}return dp[T.length()][S.length()];}}
转载于:https://www.cnblogs.com/yangykaifa/p/6903810.html
[LeetCode]Distinct Subsequences,解题报告相关推荐
- [LeetCode] Multiply Strings 解题报告
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- Leetcode Weekly 188 解题报告
文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- leetcode Distinct Subsequences
字符串处理与二维dp 设置二维数组v[i][j]表示S串中前i个字母的子串中包含多少个T串中前j个字母的子串"这样的问题记为A[i][j]. 可以得到递推式 : if(S[i-1] == T ...
- LeetCode: Add Binary 解题报告
Add Binary Given two binary strings, return their sum (also a binary string). For example, a = " ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode:115. Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A ...
- leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划)
题目 https://leetcode.com/problems/distinct-subsequences/ 题解 方法1:递归(超时) 这种解法比较容易理解,时间复杂度没算出来,但肯定不是 O(m ...
最新文章
- 【Spring Web MVC】Spring Web MVC 注解开发环境搭建
- Row_number () over (partition by col1 order by col2)的用法
- assertionerror python_Python成为专业人士笔记–内置模块Modules和函数Functions
- 365赚钱养猫小程序
- 33 关 Python 游戏,测试你的爬虫能力到底及格不?
- 32驱动_轻松掌握pinctrl子系统驱动开发——一个虚拟pinctrl dev驱动开发
- 欢迎使用CSDN-markdown编辑器--样例
- 一本用户体验时代的产品生存指南
- Flutter监听网络变化
- 数据库优化之MySQL
- MacOS怎么使用分区加密功能?MacOS硬盘分区加密功能使用方法
- 2013年深圳百公里徒步感悟
- [week13] 2 - T1
- 看点直播抓取视频回放链接
- 大学计算机基础--1
- linux虚拟机中通过挂载iso文件安装jdk
- oracle批量粘贴文本,ORACLE 快速批量导入文本数据到数据库(sqlldr工具)方法与分析...
- linux创建xfs文件系统命令,通过案例学习xfs文件系统相关命令
- Vue Cli 3项目 使用融云IM实现聊天功能
- Arduino UNO +74HC164流水灯示例
热门文章
- linux的xmgrace无法运行,科学网—安装xmgrace - 林绪波的博文
- 做小程序的流程总结(基本篇)
- localStorage和sessionStorage的简单使用
- Hibernate--使用xml配置映射关系
- Eclips将lib打入war中
- ubuntu 10.4非法关机后上不了网
- libev源码分析--常用的watcher
- [译作]Class in Jscript Part I
- linux脚本语句,LINUX shell 脚本语句
- 是先设计mysql表再进行php代码_PHP与RBAC设计思路,数据表设计与源码讲解