博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode/LintCode] Is Subsequence
阅读量:6482 次
发布时间:2019-06-23

本文共 1270 字,大约阅读时间需要 4 分钟。

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) 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?

Solution

Basic greedy

class Solution {    public boolean isSubsequence(String s, String t) {        if (s == null) return true;        else if (t == null || t.length() < s.length()) return false;        int i = 0, j = 0;        while (i < s.length() && j < t.length()) {            if (s.charAt(i) == t.charAt(j)) {                i++;                j++;            } else {                j++;            }        }        return i == s.length();    }}

Follow up solution

转载地址:http://sfbuo.baihongyu.com/

你可能感兴趣的文章
转: 将Eclipse代码导入到AndroidStudio的两种方式
查看>>
MyBatis日期有坑
查看>>
kettle插入/更新
查看>>
PHP的压力测试工具ab.exe 和mpm介绍提高并发数
查看>>
Atitit.log日志技术的最佳实践attilax总结
查看>>
Jedis(Java+Redis) Pool的使用
查看>>
撸一段 SQL ? 还是撸一段代码?
查看>>
30 个java编程技巧(最佳实践的初学者)
查看>>
php : 匿名函数(闭包) [二]
查看>>
上海某(hong)冠笔试题
查看>>
CROC 2016 - Final Round [Private, For Onsite Finalists Only] C. Binary Table FWT
查看>>
[经验] Win7减肥攻略(删文件不删功能、简化优化系统不简优化性能)
查看>>
【转载】容器技术 & Docker & 与虚拟化的比较
查看>>
朴素贝叶斯分类器
查看>>
数据库iops的理解
查看>>
redis 安装
查看>>
C#对图片文件的压缩、裁剪操作初探
查看>>
JsonPath 语法 与 XPath 对比
查看>>
计算机科学方面的学术会议
查看>>
开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)...
查看>>