-
Notifications
You must be signed in to change notification settings - Fork 0
392. Is Subsequence #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| ++it; | ||
| } | ||
| if (it == s.end()) { | ||
| break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここで return true、最後で return false してもよいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実は最初そのように書いたのですが、leetcode上でs = "", t = ""のテストケースがあり、それに対応するためにこのようにしていました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そのテストケース気付きませんでした。ありがとうございます。
| }; | ||
| ``` | ||
|
|
||
| イテレーターで書くよりintとかsize_tでインデックスを保存してoperator[]で要素比較したほうが読みやすいですかね? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思いました。
イテレーターで書くメリットは、string& 以外の型に拡張する時に楽、添字で配列外参照しない、ぐらいでしょうか。
There was a problem hiding this comment.
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|))の他に、 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一応、O(|s| log|t|))ですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、ご指摘の通りですね。失礼しました。
|
読みやすいと思います。 |
ryosuketc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
https://leetcode.com/problems/is-subsequence/description/