Skip to content

Create FirstUniqueCharacterInAString.md#12

Open
kt-from-j wants to merge 1 commit intomainfrom
FirstUniqueCharacterInAString
Open

Create FirstUniqueCharacterInAString.md#12
kt-from-j wants to merge 1 commit intomainfrom
FirstUniqueCharacterInAString

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
387. First Unique Character in a String
次に解く問題:
560. Subarray Sum Equals K

Comment on lines +57 to +58
- この実装って入力が"😀a😀"みたいな場合に2を返すことになるが、それでいいのかは要相談かも
- 見た目上は2文字目なので1を返す方が多くの場合、自然に思われる。
Copy link

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

Copy link
Owner Author

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/

JavaではJava9から正規表現に任意のUnicode拡張書記素クラスタにマッチする「\X」とUnicode拡張書記素クラスタ境界にマッチする「\b{g}」が導入されました。

```java
class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> charToFoundCount = new HashMap<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charToCountでも伝わりそうなので私だったらそうするかもしれません。今のままでも読みやすいので好みかなと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants