资讯专栏INFORMATION COLUMN

Is Subsequence

liaosilzu2007 / 1461人阅读

摘要:题目链接只要里面当前的字符可以和里的字符匹配,的就很长,用字典存起来很大,用

Is Subsequence

题目链接:https://leetcode.com/problems...

greedy, 只要s里面当前的字符可以和t里的字符匹配,s的index就+1

public class Solution {
    public boolean isSubsequence(String s, String t) {
        // 2 points, greedy
        int i = 0, j = 0;
        
        while(i < s.length() && j < t.length()) {
            if(s.charAt(i) == t.charAt(j)) i++;
            j++;
        }
        
        return i == s.length();
    }
}

follow up: string很长,用字典存起来
dict很大,用trie

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66606.html

相关文章

  • [LintCode] Longest Increasing Subsequence

    Problem Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification Whats the definition of longest increasing subsequence?...

    Flands 评论0 收藏0
  • [LeetCode/LintCode] Is Subsequence

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

    terasum 评论0 收藏0
  • leetcode392. Is Subsequence

    摘要:题目要求如何判断字符串是否是字符串的一个子序列。子序列是指中的字母均按照相对位置存在于中,比如是的一个子序列,但是就不是的一个子序列。可以看到我们能够找到一个合法的序列,使得当前字母的起始下标始终大于上一个字母的下标。 题目要求 Given a string s and a string t, check if s is subsequence of t. You may assum...

    youkede 评论0 收藏0
  • leetcode376. Wiggle Subsequence

    摘要:题目要求扭动序列是指数组中的相邻两个元素的差保证严格的正负交替,如数组中相邻两个元素的差为,满足扭动序列的要求。现在要求从一个数组中,找到长度最长的扭动子序列,并返回其长度。即前一个元素和当前元素构成下降序列,因此代码如下 题目要求 A sequence of numbers is called a wiggle sequence if the differences between ...

    CoffeX 评论0 收藏0
  • LeetCode[300] Longest Increasing Subsequence

    摘要:再用二分法找当前值应该在排好序的数组中的插入位置。因为要找的是最长的序列,所以每次将排好序的数组中替换成已经排好序的,会能保证得到的结果是最长的。保证升序相等也要替换这个值 LeetCode[300] Longest Increasing Subsequence Given an unsorted array of integers, find the length of longe...

    blankyao 评论0 收藏0
  • [LeetCode] 300. Longest Increasing Subsequence

    Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7...

    luckyyulin 评论0 收藏0

发表评论

0条评论

liaosilzu2007

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<