Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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を返す方が多くの場合、自然に思われる。
Comment on lines +57 to +58
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<文字、登場回数>
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<>();
Copy link

Choose a reason for hiding this comment

The 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;
}
}
```