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

题意及分析:给出一个字符串s,给出一个字符串t,求s是否是t中的一个序列。就是要判断s中的每一个字符在t中是否顺序存在。这道题是没看答案作出的第一到贪心算法的题。。。以上面的Example作分析,在t中先找到字符a,那么在t中找到b,c的概率就更大;对于剩下的"bc",先在t中找到b,剩下的字符就越多,找到c的概率就越大。所以,这里我们对于s中的每一个字符都尽量在t中先找到,那么剩下的字符就越多,符合要求的概率就越大。

代码:

public class Solution {
    public boolean isSubsequence(String s, String t) {
        int sLength=s.length();		//s的长度
        int tLength=t.length();	//t的长度

        int sIndex=0;		//记录找到了s的第几个字符
        int tIndex=0;		//记录遍历到了t的第几个字符

        while(tIndex<tLength){		//这里用到的贪心算法,就是在t中越先找到 s的字符 那么在t中就更容易的找到 s剩下的字符
        	if(sIndex<sLength&&(s.charAt(sIndex)==t.charAt(tIndex))){		//找到一个相等的字符就查找s的下一个字符
        		sIndex++;
        	}
        	tIndex++;
        }
        if(sIndex==sLength){		//从前往后在t中找到了s的所有字符 。所以存在
        	return true;
        }
        else{
        	return false;
        }
    }
}

  

时间: 2024-10-10 22:39:43

[LeetCode] 392. Is Subsequence Java的相关文章

(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

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

LeetCode OJ - Distinct Subsequence

这道题采用动态规划,可是我一开始没有想到.后来参考了discuss中前辈的代码和思路,才想通的. 方法二是因为每一步只和上一步的内容相关,所以可以只用O(n)的空间复杂度. 下面是AC代码: 1 /** 2 * Solution DP 3 * we keep a m*n matrix and scanning through string T, 4 * p[i][j] means the number of distinct subsequence of S(0...j) equal to T(

Leetcode: Longest Palindromic Substring. java

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划public class Solution { public String longestPalindrome(String s) { if

LeetCode | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列[Easy][Python][双指针] Problem LeetCode 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

LeetCode Longest Uncommon Subsequence II

原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of o

leetcode 61 Rotate List ----- java

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题目意思不难,就是说给一个数k,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len

[LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq

[LeetCode] Increasing Triplet Subsequence 递增的三元子序列

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return