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.

算法分析

该问题的算法还是很简单的,直接从s和t的首端开始遍历比较字符是否相等即可,如果不等,则增加在t中的下标位置;如果相等,则同时增加在s和t中的下标位置。如果t中的指标位置增长到了t的末尾,而s中的指标还没有增长的末尾,则返回false。如果s中的指标先增长到了末尾,则返回true。

Java算法实现

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

LeetCode赛题392---- Is Subsequence的更多相关文章

  1. LeetCode算法题-Longest Harmonious Subsequence(Java实现)

    这是悦乐书的第270次更新,第284篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是594).我们定义一个和谐数组是一个数组,其最大值和最小值之间的差 ...

  2. LeetCode算法题-Longest Uncommon Subsequence I(Java实现)

    这是悦乐书的第252次更新,第265篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第119题(顺位题号是521).给定一组两个字符串,您需要找到这组两个字符串中最长的不同 ...

  3. LeetCode赛题515----Find Largest Element in Each Row

    问题描述 You need to find the largest element in each row of a Binary Tree. Example: Input: 1 / \ 2 3 / ...

  4. LeetCode赛题----Find Left Most Element

    问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...

  5. LeetCode赛题395----Longest Substring with At Least K Repeating Characters

    395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...

  6. LeetCode赛题394----Decode String

    394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...

  7. LeetCode赛题393----UTF-8 Validation

    393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...

  8. LeetCode赛题391----Perfect Rectangle

    #391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...

  9. LeetCode赛题390----Elimination Game

    # 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...

随机推荐

  1. servlet,listener,filter,interceptor的关系

    1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层.最早支持 Servlet 技术 ...

  2. BZOJ - 1009 KMP+可达矩阵

    题意:存在一个长度为m的串str,求长度为n的不含str子串的字符串的方案数 什么鬼题目 设\(f[i][j]\):长为\(i\)的串中以\(i\)结尾的长度为\(j\)的后缀 与 模式串(str)中 ...

  3. normalize.css源码解析

    什么是normalize.css?  它是为了帮助我们统一各个浏览器的样式和消除bug的css库. 为什么需要normalize.css,有什么好处? 不像一些reset.css,normalize. ...

  4. JavaScript设计模式-15.适配器模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. WPF简单MVVM实现

    1. MVVM介绍: MVVM就是: Model -- 模型(现实中对象的抽象) View -- UI(用户界面) ViewModel -- UI界面的抽象(给View提供数据,并响应View的操作) ...

  6. leetcode:N-Queens 问题

    一.N-QueensII class Solution { public: int totalNQueens(int n) { ; vector<); dfs(,n,total,v); retu ...

  7. sourceTree git 空目录从远程仓库克隆代码出现warning: templates not found

    解决办法: 在安装git时没有默认安装到c盘,而是安装到了d盘.在使用SourceTree进行代码克隆时提示warning: templates not found in D:\software\de ...

  8. weblogic升级之ddconverter

    1. weblogic8.x 升到weblogic10时,需要升级ejb响应的描述符,否则会报错. BEA-011114 - Error: For EJB modules, deployment pl ...

  9. [译]用R语言做挖掘数据《三》

    决策树和随机森林 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到 ...

  10. Details.cshtml(118): error CS1001: 应输入标识符

    写了没定义 @Html.DisplayFor(model => model.)