Open
Conversation
nanae772
reviewed
Oct 6, 2025
Comment on lines
+57
to
+58
| - この実装って入力が"😀a😀"みたいな場合に2を返すことになるが、それでいいのかは要相談かも | ||
| - 見た目上は2文字目なので1を返す方が多くの場合、自然に思われる。 |
There was a problem hiding this comment.
私もこの問題で知ったのですがこういう「見た目の一文字」をグラフィムクラスタ(grapheme cluster)とか書記素クラスタと呼ぶみたいですね。Javaでグラフィムクラスタを扱うライブラリがあるか調べてみてもよいかもしれません。
また以下のnoteも参考になりました、よければご覧ください。
https://note.com/ttuusskk/n/n1bff5d8e638c
Owner
Author
There was a problem hiding this comment.
グラフィムクラスタ(grapheme cluster)という名前なのですね。
調べてみたらjava20から標準的なライブラリ(java.text. BreakIterator)でグラフィムクラスタに対応した文字数を確認できるようです。
https://bugs.openjdk.org/browse/JDK-8292992
そのほか、正規表現で文字間をマッチするという手段もあるようでした。
https://www.hos.co.jp/blog/20211122/
JavaではJava9から正規表現に任意のUnicode拡張書記素クラスタにマッチする「\X」とUnicode拡張書記素クラスタ境界にマッチする「\b{g}」が導入されました。
| ```java | ||
| class Solution { | ||
| public int firstUniqChar(String s) { | ||
| Map<Character, Integer> charToFoundCount = new HashMap<>(); |
There was a problem hiding this comment.
charToCountでも伝わりそうなので私だったらそうするかもしれません。今のままでも読みやすいので好みかなと思います。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
今回解いた問題:
387. First Unique Character in a String
次に解く問題:
560. Subarray Sum Equals K