diff --git a/387. First Unique Character in a String.md b/387. First Unique Character in a String.md new file mode 100644 index 0000000..8432ef2 --- /dev/null +++ b/387. First Unique Character in a String.md @@ -0,0 +1,100 @@ +URL: https://leetcode.com/problems/kth-largest-element-in-a-stream/description/ + +# Step 1 + +- 実装時間: 10分 +- len(s)をnとして + - 時間計算量: O(n) + - 空間計算量: O(n) + +```python +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_count = Counter(s) + for i, char in enumerate(s): + if char_to_count[char] == 1: + return i + return -1 +``` + +- characterというより、C言語のchar型のイメージであえて英単語の省略形を選んだ + +# Step 2 + +- 参考にしたURL + - https://github.com/Exzrgs/LeetCode/pull/9 + - https://github.com/SuperHotDogCat/coding-interview/pull/22 + - https://github.com/hayashi-ay/leetcode/pull/28/files + - https://github.com/su33331/practice/pull/2 + - https://github.com/fhiyo/leetcode/pull/18 + - https://github.com/TORUS0818/leetcode/pull/17 + - https://github.com/nittoco/leetcode/pull/20 + - https://github.com/kazukiii/leetcode/pull/16 + - https://github.com/Mike0121/LeetCode/pull/32 + - https://github.com/Yoshiki-Iwasa/Arai60/pull/14 + - https://github.com/seal-azarashi/leetcode/pull/15 + - https://github.com/hroc135/leetcode/pull/15 + - https://github.com/ryoooooory/LeetCode/pull/20 + - https://github.com/tarinaihitori/leetcode/pull/15 + - https://github.com/philip82148/leetcode-arai60/pull/4 + - https://github.com/colorbox/leetcode/pull/29 + - https://github.com/Hurukawa2121/leetcode/pull/15 + +- 追加質問でありそうだなと思ったやつ + - Counterを使わずに書くと? + - 自分で辞書を作る + - その場合、defaultdictを使わずに書くと? + - 26文字char配列を使う方法 + - 辞書を作らないのであれば、左右から検索して最初に見つかった位置が一致していればunique, という探し方もできる。 + +```python +class Solution: + def firstUniqChar(self, s: str) -> int: + for c in s: + if s.rindex(c) == s.index(c): + return s.index(c) + return -1 +``` + +- > `return next(iter(char_to_first_appear_index.values()))` + - 一瞬、「リストにして`[0]`でいいじゃん?」と思ったけど、リストを作らずにiteratorのまま次の要素だけ持ってきているので効率的。 + - 見たことない書き方だったけど、効率的な書き方だと思って感動。 + - https://github.com/hayashi-ay/leetcode/pull/28/files + +- > c は、私は許容です。char は、C/C++/Java などで予約語なので、私は避けます。 + - 僕がstep1を解いてる時も、「文字コードから文字に変換するbuilt-inの名前はchrだっけcharだっけ。それと被らないようにしないと。」と、一度考えてた。これは脳のリソースを使ってるということ。 + - https://github.com/su33331/practice/pull/2/files#r1615489250 + +- > -1 がどのような意味を持つのかコードを見るだけだと分からないので、定数にして役割に準じた名前をつけたり、コードコメントを残すといったことをした方がいいように思いました。 + - https://github.com/Exzrgs/LeetCode/pull/9/files#r1703858740 + - > `return -1; // Not Found` + - https://github.com/colorbox/leetcode/pull/29/files#r1861038507 + +- grueやbleenのはなし + - https://github.com/fhiyo/leetcode/pull/18/files#r1629654317 + +```python +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_frequency = Counter(s) + for i, c in enumerate(s): + if char_to_frequency[c] == 1: + return i + return -1 # Not found +``` + +# Step 3 + +- len(s)をnとして + - 時間計算量: O(n) + - 空間計算量: O(n) + +```python +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_frequency = Counter(s) + for i, c in enumerate(s): + if char_to_frequency[c] == 1: + return i + return -1 # Not found +```