Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 25, 2025

++it;
}
if (it == s.end()) {
break;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで return true、最後で return false してもよいと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実は最初そのように書いたのですが、leetcode上でs = "", t = ""のテストケースがあり、それに対応するためにこのようにしていました。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そのテストケース気付きませんでした。ありがとうございます。

};
```

イテレーターで書くよりintとかsize_tでインデックスを保存してoperator[]で要素比較したほうが読みやすいですかね?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思いました。
イテレーターで書くメリットは、string& 以外の型に拡張する時に楽、添字で配列外参照しない、ぐらいでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに今回でいうs, tなどの変数名に応じてプログラムを変えなくていいのはメリットですね!

Follow upあるの気づかなかった。
Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?

tにおいてある文字が出てくるindexの配列を<char, vector<int>>の辞書として管理しておいて、現在の場所以降で一番最初にあるs中の文字が出てくる場所を二分探索で探す方法(1つのsあたりの時間計算量O(|s|log|s|))の他に、

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一応、O(|s| log|t|))ですね。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、ご指摘の通りですね。失礼しました。

@skypenguins
Copy link

読みやすいと思います。

Copy link

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants