-
Notifications
You must be signed in to change notification settings - Fork 0
Create FirstUniqueCharacterInAString.md #12
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
kt-from-j
wants to merge
1
commit into
main
Choose a base branch
from
FirstUniqueCharacterInAString
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
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
102 changes: 102 additions & 0 deletions
102
387. First Unique Character in a String/FirstUniqueCharacterInAString.md
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,102 @@ | ||
| # step1 何も見ずに解く | ||
| - 各文字について、初登場のインデックス・登場回数を知りたい | ||
| - Keyは文字としてMapのvalueとして{firstFoundIndex, foundCount}を保持すればいいのだが、クラスを宣言するほどでもないかと思いやめた | ||
| ## 解答 | ||
| - Mapを2つ使用した実装 | ||
| - `charToFirstFoundIndex`無しで`s.indexOf()`やsの要素に対するforループで十分だったかも | ||
| ```java | ||
| class Solution { | ||
| public int firstUniqChar(String s) { | ||
| // Map<文字、登場回数> | ||
| Map<Character, Integer> charToFoundCount = new LinkedHashMap<>(); | ||
| // Map<文字、初登場のインデックス> | ||
| Map<Character, Integer> charToFirstFoundIndex = new HashMap<>(); | ||
|
|
||
| char[] chars = s.toCharArray(); | ||
| for (int i = 0; i < chars.length; i++) { | ||
| char c = chars[i]; | ||
| charToFoundCount.merge(c, 1, Integer::sum); | ||
| if (!charToFirstFoundIndex.containsKey(c)) { | ||
| charToFirstFoundIndex.put(c, i); | ||
| } | ||
| } | ||
|
|
||
| for (Map.Entry<Character, Integer> entry : charToFoundCount.entrySet()) { | ||
| if (entry.getValue() == 1) { | ||
| return charToFirstFoundIndex.get(entry.getKey()); | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
| ``` | ||
| - Set1つの実装 | ||
| - forループは一つだけど、lastIndexOfしているから最悪計算量はO(n^2)になるのかな | ||
| ```java | ||
| class Solution { | ||
| public int firstUniqChar(String s) { | ||
| char[] chars = s.toCharArray(); | ||
| Set<Character> foundChar = new HashSet<>(); | ||
| for (int i = 0; i < chars.length; i++) { | ||
| char c = chars[i]; | ||
| if (i == s.lastIndexOf(c) && !foundChar.contains(c)) { | ||
| return i; | ||
| } | ||
| foundChar.add(c); | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # step2 他の方の解答を見る | ||
| ## 解答 | ||
| - https://github.com/jeymak-trainee/arai60training/pull/4 | ||
| - サロゲートペアに対応した実装 | ||
| - 次の文字に移る際に`i += Character.charCount(codePoint);`としなければいけない点に注意が必要 | ||
| - この実装って入力が"😀a😀"みたいな場合に2を返すことになるが、それでいいのかは要相談かも | ||
| - 見た目上は2文字目なので1を返す方が多くの場合、自然に思われる。 | ||
| ```java | ||
| class Solution { | ||
| public int firstUniqChar(String s) { | ||
| // Map<文字、登場回数> | ||
| Map<Integer, Integer> charToFoundCount = new LinkedHashMap<>(); | ||
| for (int i = 0; i < s.length();) { | ||
| int codePoint = s.codePointAt(i); | ||
| charToFoundCount.merge(codePoint, 1, Integer::sum); | ||
| i += Character.charCount(codePoint); | ||
| } | ||
|
|
||
| for (int i = 0; i < s.length();) { | ||
| int codePoint = s.codePointAt(i); | ||
| if (charToFoundCount.get(codePoint) == 1) { | ||
| return i; | ||
| } | ||
| i += Character.charCount(codePoint); | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # step3 3回ミスなく書く | ||
| - 繰り返し書くと面倒になり、Step1をシンプルにした実装で書いた | ||
| ## 解答 | ||
| ```java | ||
| class Solution { | ||
| public int firstUniqChar(String s) { | ||
| Map<Character, Integer> charToFoundCount = new HashMap<>(); | ||
|
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. charToCountでも伝わりそうなので私だったらそうするかもしれません。今のままでも読みやすいので好みかなと思います。 |
||
| for (Character c : s.toCharArray()) { | ||
| charToFoundCount.merge(c, 1, Integer::sum); | ||
| } | ||
| for (int i = 0; i < s.length(); i++) { | ||
| Character c = s.charAt(i); | ||
| if (charToFoundCount.get(c) == 1) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
| ``` | ||
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.
私もこの問題で知ったのですがこういう「見た目の一文字」をグラフィムクラスタ(grapheme cluster)とか書記素クラスタと呼ぶみたいですね。Javaでグラフィムクラスタを扱うライブラリがあるか調べてみてもよいかもしれません。
また以下のnoteも参考になりました、よければご覧ください。
https://note.com/ttuusskk/n/n1bff5d8e638c
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.
グラフィムクラスタ(grapheme cluster)という名前なのですね。
調べてみたらjava20から標準的なライブラリ(java.text. BreakIterator)でグラフィムクラスタに対応した文字数を確認できるようです。
https://bugs.openjdk.org/browse/JDK-8292992
そのほか、正規表現で文字間をマッチするという手段もあるようでした。
https://www.hos.co.jp/blog/20211122/