森田賢二のleetCodeの使い方、解説、解答を毎日更新しているキャラクター

[leetcode]問題解答と解説。392. Is Subsequence [JavaScript]

[leetcode]問題解答と解説。392. Is Subsequence [JavaScript]

皆さんleetCode好きですよね。私も好きです。
なかなかやる機会がないのですよね。
leetCodeの使い方や解説ではなく実際に問題を解決するために何をしたか
をメモ書きして行きます

私はある決意をして今挑んでいます。
モチベーションを保つためだけにこれをやっています。

では今日のleetCode。
【392. Is Subsequence】の解決策と方法。自分がまず挑戦して
やったことをさらけ出します。

※下にある自分のコードは自分で設けた制限時間内に書けたところまです。

これは
「私は年末までこれを続けたらどんなleetCoderになるか」のシリーズ。
実験的ブログ更新です。

森田賢二のleetCode
森田賢二のleetCode

[leetcode] 392. Is Subsequence ルール

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

2つの文字列sとtが与えられたとき、sがtの部分配列であればtrueを、そうでなければfalseを返す。

文字列の部分配列とは、元の文字列から、残りの文字の相対的な位置を乱すことなく、いくつかの文字(なくてもよい)を削除してできた新しい文字列のことである。(例: "ace" は "abcde" の部分文字列であるが、"aec" は部分文字列ではない)。

Example 1:

Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"
Output: false

exampleは実際の問題ページを見てください

[leetcode] 392. Is Subsequence 問題ページ

longest-palindromic-substring

[leetcode] 392. Is Subsequence

my code is good

function isSubsequence(s: string, t: string): boolean {
    let ls = s.length-1, lt = t.length-1;
    let i = 0, j = 0
    while(i <= ls && j <= lt){
        if(s[i] === t[j]){
            i++
            j++
            continue;
        }
        j++
    }
    if(i > ls) return true
    return false
};

これはイメージ通りでした

[leetcode] 392. Is Subsequence。discussの中の一つの解答例

数ある中からJavaScriptのもので、理解しやすい解説をピックアップしました。
discussから見ればさらにもっと違う方法で真似したくなるものがあるかもしれない

JavaScriptでfilterされている、discussはこちらから

[leetcode] 392. Is Subsequenceをやってみて感想

  • これはちゃんとイメージ通りだった

カテゴリー leetCode