-
Notifications
You must be signed in to change notification settings - Fork 0
387. First Unique Character In A String #17
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
Open
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
387
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| ## step1 | ||
| - charactorがsの中に入っているかを愚直に見ていくのは? | ||
| - 10^5の場合setにしても大丈夫な大きさなはず | ||
| - 文字列が含んでいるかのinの判定は早いと聞いたことがあるが、内部の実装まで知らないのでstep2で調べたい | ||
| - 計算量はO(N^2) | ||
| - Counterの方が確実かもしれない | ||
| - Counterの場合はO(N) | ||
| - 見つからなかった場合の-1は定数(NO_UNIQUE_VALUE_RESPONSEのような感じ?)に入れておくべきか迷った | ||
| - そもそも-1を返すのはやめたい気持ちがある(Noneの方が後々indexとして使おうとするとエラーになるのでいいのではないかと思っている) | ||
|
|
||
| ## step2 | ||
| - chaが組み込み関数なのでcとしたが、charの方が良かったかもしれない | ||
| - 他の人のコードをみる | ||
| - https://github.com/Hiroto-Iizuka/coding_practice/pull/15/files | ||
| - `この問題は LRU に類似したデータ構造を私は連想しましたが、しなくてもいいかの境界くらいです。よく聞かれるので知っていてもいいかもしれません。(よく聞かれる理由は知らなくても作れていいだろうと思われるからですね。)` | ||
| - https://github.com/mamo3gr/arai60/pull/15/files | ||
| - `next`や`iter`に馴染みが無かったので調べる | ||
| - https://docs.python.org/ja/3/library/functions.html#next | ||
| - https://docs.python.org/ja/3/library/functions.html#iter | ||
| - こういうのを使っていると上級者なんだろうなと思う | ||
| - 自分から書く選択肢に全くないのでなんとかしたい気持ち | ||
| - https://github.com/yas-2023/leetcode_arai60/pull/15/files | ||
| - ベンチマークすごい | ||
| - 自分もloopの際に省略せずindex, characterと書いた方が良いかも | ||
| - inを使うよりもfindとrfindの方がネイティブで実行されるため良いのか? | ||
| - https://docs.python.org/ja/3/library/stdtypes.html#str.find | ||
| - `注釈 find() メソッドは、 sub の位置を知りたいときにのみ使うべきです。 sub が部分文字列であるかどうかのみを調べるには、 in 演算子を使ってください:` | ||
| - inは内部では`文字列やバイト列型については、 x in y は x が y の部分文字列であるとき、かつそのときに限り True になります。これは y.find(x) != -1 と等価です。空文字列は、他の任意の文字列の部分文字列とみなされます。従って "" in "abc" は True を返すことになります。`なので速度的には変わらないみたい | ||
| - `この方法ですと、入力が"aaaaaa....aab"みたいな時にforループが何度もaをチェックしてしまうため、個人的にはStep 1のように文字ごとにfirst indexを覚えておく方が好ましいと思います。計算量は同じO(n)ですが、場合によっては2倍ほどになる気がします。` | ||
| - counter作るのでO(N)でloopで0(N)なので実質2回forのloopをまわしているが、2回目は種類が少ない文字列側で見ていくと確かに良さそうだと思った | ||
| - https://github.com/Yuto729/LeetCode_arai60/pull/20/files | ||
| - 上記のコメントで出てきたLRUライクの意味が実装を見てわかった | ||
| - 2回目に出てきた時にdictのkeyを消して、あとはseenにあるかを見て最後に一番indexが早いものからdictに追加されたはずなので.values()で取り出す流れ | ||
| - Counterも追加した順番が保持されるのかは気になったので後で調べる | ||
| - `Counter は ハッシュ可能 なオブジェクトをカウントする dict のサブクラスです。これは、要素を辞書のキーとして保存し、そのカウントを辞書の値として保存するコレクションです。カウントは、0 や負のカウントを含む整数値をとれます。 Counter クラスは、他の言語のバッグや多重集合のようなものです。` | ||
| - https://docs.python.org/ja/3/library/collections.html#collections.Counter | ||
|
|
||
| - step2に1回だけ走査する方法(1passというらしい) | ||
| - O(N) | ||
| - step2-2にCounterで辞書の入力順を利用した文字列側でのloopで解く方法を書いてみる | ||
| - カウンター作成でO(N), 文字のloopでO(M), findでO(N) Mは文字の種類 | ||
|
|
||
| ## step3 | ||
| - 1passの方法とカウンターの方法で2回ずつ書いてみる | ||
| - カウンターの方法の方が見た時に何しているか理解するのに時間がかからない気がする | ||
| - step2の時に他の人のコード見て思ったが、誰も見つからなかった時の-1を定数に入れたりしてなかったのでしないのが普通の感覚なのかと思った | ||
| - 自分も普段は絶対しないと思うが、いきなり−1が出てきて減点されないかどうかが少し心配だった | ||
|
|
||
21 changes: 21 additions & 0 deletions
21
problems/387.first-unique-character-in-a-string/step1-2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # | ||
| # @lc app=leetcode id=387 lang=python3 | ||
| # | ||
| # [387] First Unique Character in a String | ||
| # | ||
|
|
||
| # @lc code=start | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| charactor_counter = collections.Counter(s) | ||
| for i, c in enumerate(s): | ||
| if charactor_counter[c] == 1: | ||
| return i | ||
|
|
||
| return -1 | ||
|
|
||
|
|
||
| # @lc code=end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # | ||
| # @lc app=leetcode id=387 lang=python3 | ||
| # | ||
| # [387] First Unique Character in a String | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| for i, c in enumerate(s): | ||
| if c not in s[:i] and c not in s[i + 1 :]: | ||
| return i | ||
|
|
||
| return -1 | ||
|
|
||
|
|
||
| # @lc code=end |
22 changes: 22 additions & 0 deletions
22
problems/387.first-unique-character-in-a-string/step2-2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # | ||
| # @lc app=leetcode id=387 lang=python3 | ||
| # | ||
| # [387] First Unique Character in a String | ||
| # | ||
|
|
||
| # @lc code=start | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| charactor_counter = collections.Counter(s) | ||
|
|
||
| for charactor, count in charactor_counter.items(): | ||
| if count == 1: | ||
| return s.find(charactor) | ||
|
|
||
| return -1 | ||
|
|
||
|
|
||
| # @lc code=end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # | ||
| # @lc app=leetcode id=387 lang=python3 | ||
| # | ||
| # [387] First Unique Character in a String | ||
| # | ||
|
|
||
| # @lc code=start | ||
|
|
||
|
|
||
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| charactor_to_index = dict() | ||
| seen_charactors = set() | ||
|
|
||
| for index, charactor in enumerate(s): | ||
| if charactor in charactor_to_index: | ||
| del charactor_to_index[charactor] | ||
| continue | ||
|
|
||
| if charactor in seen_charactors: | ||
| continue | ||
|
|
||
| charactor_to_index[charactor] = index | ||
| seen_charactors.add(charactor) | ||
|
|
||
| if charactor_to_index: | ||
| return next(iter(charactor_to_index.values())) | ||
|
|
||
| return -1 | ||
|
|
||
|
|
||
| # @lc code=end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||
| # | ||||||
| # @lc app=leetcode id=387 lang=python3 | ||||||
| # | ||||||
| # [387] First Unique Character in a String | ||||||
| # | ||||||
|
|
||||||
|
|
||||||
| # @lc code=start | ||||||
| class Solution: | ||||||
| def firstUniqChar(self, s: str) -> int: | ||||||
| charactor_to_index = dict() | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. タイポしてますね。
Suggested change
|
||||||
| seen_charactors = set() | ||||||
|
|
||||||
| for index, charactor in enumerate(s): | ||||||
| if charactor in charactor_to_index: | ||||||
| del charactor_to_index[charactor] | ||||||
| continue | ||||||
|
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 頭から読んだとき、
のように、実際の動きを頭の中で頑張ってシミュレートする必要があり、短い行数のわりに大変に感じました。 |
||||||
| if charactor in seen_charactors: | ||||||
| continue | ||||||
|
|
||||||
| charactor_to_index[charactor] = index | ||||||
| seen_charactors.add(charactor) | ||||||
|
|
||||||
| if charactor_to_index: | ||||||
| return next(iter(charactor_to_index.values())) | ||||||
|
|
||||||
| return -1 | ||||||
|
|
||||||
|
|
||||||
| # @lc code=end | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
私も知らなくて、調べて見つけた記憶があります。
dict.values()の一番目だけ欲しいけど、dict[1]とは書けないし、リストにしたら全要素コピーされて無駄だぞ…。何か方法があるんじゃないかな、と思って調べて突き当たりました。私の経験からですが「何か方法があるんじゃないかな」は大抵の場合、方法があります。