[LC] 392. Is Subsequence

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.

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

class Solution {
    public boolean isSubsequence(String s, String t) {
        if (s == null || s.length() == 0) {
            return true;
        }
        int index = 0;
        for (char ch : t.toCharArray()) {
            if (index < s.length() && s.charAt(index) == ch) {
                index += 1;
            }
        }
        return index == s.length();
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/11840214.html

时间: 2024-10-31 07:30:41

[LC] 392. Is Subsequence的相关文章

[LeetCode] 392. Is Subsequence Java

题目: 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 sis a short string (<=100). A subsequence

(Java) LeetCode 392. Is Subsequence —— 判断子序列

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 sis a short string (<=100). A subsequence of a

392. Is Subsequence - Medium

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 sand t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). A subsequence of a

392. Is Subsequence

https://leetcode.com/problems/is-subsequence/#/solutions http://www.cnblogs.com/EdwardLiu/p/6116896.html public boolean isSubsequence(String s, String t) { int j = 0, i = 0; while (j < t.length() && i < s.length()) { if (s.charAt(i) == t.cha

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

leetcode-22-string

521. Longest Uncommon Subsequence I find the longest uncommon subsequence of this group of two strings 解题思路: 因为求的是最长uncommon subsequence的长度,所以,如果ab长度不等,应返回长度较大值:ab相同,则返回-1:否则返回长度即可. int findLUSlength(string a, string b) { if (a == b) return -1; if (a

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode题解——算法思想之贪心思想

1. 分配饼干 2. 不重叠的区间个数 3. 投飞镖刺破气球 4. 根据身高和序号重组队列 5. 买卖股票最大的收益 6. 买卖股票的最大收益 II 7. 种植花朵 8. 判断是否为子序列 9. 修改一个数成为非递减数组 10. 子数组最大的和 11. 分隔字符串使同种字符出现在一起 保证每次操作都是局部最优的,并且最后得到的结果是全局最优的. 1. 分配饼干 455. Assign Cookies (Easy) Input: [1,2], [1,2,3] Output: 2 Explanati