-
Notifications
You must be signed in to change notification settings - Fork 0
Create 387. First Unique Character in a String #27
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
Apo-Matchbox
wants to merge
2
commits into
main
Choose a base branch
from
387.-First-Unique-Character-in-a-String
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
2 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,116 @@ | ||
| # [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/description/) | ||
| ## Step1 | ||
|
|
||
| ### 問題意図の考察 | ||
| - 問題文の確認 | ||
| 1. 文字列sが与えられ、繰り返し使用されていない初めの文字があれば、そのindexを返す | ||
| 2. 存在しなければ、-1を返す | ||
|
|
||
| - 制約の確認 | ||
| 1. 1<= s.length <= 10^5 (100,000) | ||
| 2. sは、lowercase English lettersのみ 26文字 | ||
|
|
||
| ### 解法を考える。 | ||
| - 初めの文字を保持して、それぞれの文字と付き合わせをする方法だと全文字の検索・線形探索 O(n^2) 10^10 | ||
| - 他の方法が思いつかないので、一度これで書く | ||
|
|
||
| 初回の回答 | ||
| ```cpp | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int firstUniqChar(const std::string& s) { | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| bool unique = true; | ||
| for (int j = 0; j < s.size(); ++j) { | ||
| if (i != j && s[i] == s[j]) { | ||
| unique = false; | ||
| break; | ||
| } | ||
| } | ||
| if (unique) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Step2 | ||
| - 調べると、2回スキャンして頻度の集計を行う方法がある。1回目:頻度の集計 2回目:最初にfrequency == 1のindexを返す方法 | ||
| - mapだと、O(log n)かかる。全体では、O(n log n) | ||
|
|
||
| ```cpp | ||
| #include <map> | ||
| #include <string> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int firstUniqChar(const std::string& s) { | ||
| std::map<char, int> character_to_frequency; | ||
| for (char c : s) { | ||
| character_to_frequency[c]++; | ||
| } | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| if (character_to_frequency[s[i]] == 1) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| - この問題では、制約が26文字なので、vectorでも十分 | ||
| - ASCII全体やユニコードの扱い、キーの数が大きくなる時、mapが適切か | ||
| - vector allocator | ||
| * https://en.cppreference.com/w/cpp/container/vector/vector | ||
| - ASCII chart | ||
| * https://en.cppreference.com/w/cpp/language/ascii.html | ||
| * 大文字 -> 小文字の変換などやったのを思い出した。Printableの判定など一通り時間をとって復習したい。 | ||
|
|
||
| ```cpp | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int firstUniqChar(const std::string& s) { | ||
| std::array<int, 26> frequency{}; | ||
| for (char c : s) { | ||
| frequency[c - 'a']++; | ||
| } | ||
| for (int i = 0; i < static_cast<int>(s.size()); ++i) { | ||
|
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. ちゃんと int に static_cast していて、えらいなと思いました。 |
||
| if (frequency[s[i] - 'a'] == 1) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| - 他の方のコードを読む | ||
| * https://github.com/ryosuketc/leetcode_arai60/pull/15/commits/ef49af137afb1668a68c0910c6bf69f295fe8d79 | ||
| * https://github.com/irohafternoon/LeetCode/pull/17/commits/5c32a510c26e07bfd7bd6d0de124fc4ad0f4c95b | ||
| * https://github.com/Hurukawa2121/leetcode/pull/15/commits/282ac89bf465b718c26c24ffe60bf799b4fc4b18 | ||
| * https://github.com/kt-from-j/leetcode/pull/12/commits/d3fbd5ba09f088c358e78fecbcd8ab468cd0596e | ||
|
|
||
| - 他の解法 | ||
|
|
||
| ### データ構造とアルゴリズム | ||
| - sは、英小文字のみ('a'~'z'), 26種類なので、vector<int> frequency(26) | ||
| - 1回目のループO(n), 2回目のループO(n) で合計 O(n) | ||
|
|
||
| ## Step3 | ||
|
|
||
| 1. 4min | ||
| 2. 3min | ||
| 3. 3min | ||
| 4. 1min | ||
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.
ありがとうございます!
ご指摘の通りだと思いました!要素数が変わらない場面では固定長配列を選ぶ、という視点は今まであまり意識していなかったので勉強になりました。