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
116 changes: 116 additions & 0 deletions 387/387. First Unique Character in a String.md
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{};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!

ご指摘の通りだと思いました!要素数が変わらない場面では固定長配列を選ぶ、という視点は今まであまり意識していなかったので勉強になりました。

for (char c : s) {
frequency[c - 'a']++;
}
for (int i = 0; i < static_cast<int>(s.size()); ++i) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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