码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 392. Is Subsequence

时间:2018-12-03 22:57:07      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:origin   example   osi   without   length   not   case   tco   code   

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

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).

Example 1:
s = “abc”, t = “ahbgdc”

Return true.

Example 2:
s = “axc”, t = “ahbgdc”

Return false.

分析

这道题我先是用动态规划和二维数组来做的,但空间不够

class Solution {
public:
    bool isSubsequence(string s, string t) {
         int dp[s.size()+1][t.size()+1];
         for(int i=0;i<=t.size();i++)
             dp[0][i]=1;
         for(int i=1;i<=s.size();i++){
             dp[i][0]=0;
             for(int j=1;j<=t.size();j++) 
                 if(s[i-1]==t[j-1])
                    dp[i][j]=dp[i-1][j-1]==1?1:0;
                 else
                    dp[i][j]=dp[i][j-1];
         }
         if(dp[s.size()][t.size()]==1)
             return true;
         else
             return false;
    }
};

LeetCode 392. Is Subsequence

标签:origin   example   osi   without   length   not   case   tco   code   

原文地址:https://www.cnblogs.com/A-Little-Nut/p/10061315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!