From 8ad7a3764f3d4fb246de23ca139d3f70ff17c313 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" <huangge1199> Date: Mon, 6 Sep 2021 19:57:51 +0800 Subject: [PATCH] =?UTF-8?q?392:=E5=88=A4=E6=96=AD=E5=AD=90=E5=BA=8F?= =?UTF-8?q?=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/IsSubsequence.java | 72 +++++++++++++++++++ .../editor/cn/doc/content/IsSubsequence.md | 38 ++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/IsSubsequence.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/IsSubsequence.md diff --git a/src/main/java/leetcode/editor/cn/IsSubsequence.java b/src/main/java/leetcode/editor/cn/IsSubsequence.java new file mode 100644 index 0000000..e03fc51 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/IsSubsequence.java @@ -0,0 +1,72 @@ +//给定字符串 s 和 t ,判断 s 是否为 t 的子序列。 +// +// 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而 +//"aec"不是)。 +// +// 进阶: +// +// 如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代 +//码? +// +// 致谢: +// +// 特别感谢 @pbrother 添加此问题并且创建所有测试用例。 +// +// +// +// 示例 1: +// +// +//输入:s = "abc", t = "ahbgdc" +//输出:true +// +// +// 示例 2: +// +// +//输入:s = "axc", t = "ahbgdc" +//输出:false +// +// +// +// +// 提示: +// +// +// 0 <= s.length <= 100 +// 0 <= t.length <= 10^4 +// 两个字符串都只由小写字符组成。 +// +// Related Topics 双指针 字符串 动态规划 👍 504 👎 0 + +package leetcode.editor.cn; + +//392:判断子序列 +class IsSubsequence { + public static void main(String[] args) { + //测试代码 + Solution solution = new IsSubsequence().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public boolean isSubsequence(String s, String t) { + if (s.length() == 0) { + return true; + } + if (t.length() == 0) { + return false; + } + int index = 0; + for (int i = 0; i < t.length() && index < s.length(); i++) { + if (t.charAt(i) == s.charAt(index)) { + index++; + } + } + return index == s.length(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/IsSubsequence.md b/src/main/java/leetcode/editor/cn/doc/content/IsSubsequence.md new file mode 100644 index 0000000..776baf2 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/IsSubsequence.md @@ -0,0 +1,38 @@ +<p>给定字符串 <strong>s</strong> 和 <strong>t</strong> ,判断 <strong>s</strong> 是否为 <strong>t</strong> 的子序列。</p> + +<p>字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,<code>"ace"</code>是<code>"abcde"</code>的一个子序列,而<code>"aec"</code>不是)。</p> + +<p><strong>进阶:</strong></p> + +<p>如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?</p> + +<p><strong>致谢:</strong></p> + +<p>特别感谢<strong> </strong><a href="https://leetcode.com/pbrother/">@pbrother </a>添加此问题并且创建所有测试用例。</p> + +<p> </p> + +<p><strong>示例 1:</strong></p> + +<pre> +<strong>输入:</strong>s = "abc", t = "ahbgdc" +<strong>输出:</strong>true +</pre> + +<p><strong>示例 2:</strong></p> + +<pre> +<strong>输入:</strong>s = "axc", t = "ahbgdc" +<strong>输出:</strong>false +</pre> + +<p> </p> + +<p><strong>提示:</strong></p> + +<ul> + <li><code>0 <= s.length <= 100</code></li> + <li><code>0 <= t.length <= 10^4</code></li> + <li>两个字符串都只由小写字符组成。</li> +</ul> +<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li><li>动态规划</li></div></div><br><div><li>👍 504</li><li>👎 0</li></div> \ No newline at end of file