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.charAt(j)) {
                i++;
                j++;
            } else {
                j++;
            }

       }
       if (i == s.length()) {
          return true;
       } else {
          return false;
       }
   }

  

Follow Up:

The best solution is to create a map for String t, key is char, value is the index of appearance in ascending order

public boolean isSubsequence(String s, String t) {
        List<Integer>[] idx = new List[256]; // Just for clarity  字典集合, 每一个字母的ASCII 码当作键, 在t中的顺序当作值
        for (int i = 0; i < t.length(); i++) { // 生成字典
            if (idx[t.charAt(i)] == null)
                idx[t.charAt(i)] = new ArrayList<>();
            idx[t.charAt(i)].add(i);
        }

        int prev = 0;  // 前一个字母在t中的坐标, 控制升序
        for (int i = 0; i < s.length(); i++) {  //开始在字典里查找
            if (idx[s.charAt(i)] == null) return false; // Note: char of S does NOT exist in T causing NPE
            int j = Collections.binarySearch(idx[s.charAt(i)], prev);  //在当前字母表中查找其比之前的字母升序的坐标
            if (j < 0) j = -j - 1;                                     // 二分搜索的注意点
            if (j == idx[s.charAt(i)].size()) return false;            //在升序后找不到false
            prev = idx[s.charAt(i)].get(j) + 1;                        //在t中查找当前s的字母比s中的前一个字母在t中升序的坐标
    }     return true; }

  

时间: 2024-10-09 07:47:14

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

[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

过中等难度题目.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