Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions arai60/first-unique-character-in-a-string/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
2m30s

ループ1回で終わらせたいとは思ったが、2回にしてしまったほうがシンプルになると思ったのでそうした
"""

from collections import defaultdict

class Solution:
def firstUniqChar(self, s: str) -> int:
char_count = defaultdict(int)
for c in s:
char_count[c] += 1
for i, c in enumerate(s):
if char_count[c] == 1:
return i
return -1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

-1 がどのような意味を持つのかコードを見るだけだと分からないので、定数にして役割に準じた名前をつけたり、コードコメントを残すといったことをした方がいいように思いました。

17 changes: 17 additions & 0 deletions arai60/first-unique-character-in-a-string/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
割とみんな同じような解き方。

char_count → char_to_count
"""

from collections import defaultdict

class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_count = defaultdict(int)
for c in s:
char_to_count[c] += 1
for i, c in enumerate(s):
if char_to_count[c] == 1:
return i
return -1
Empty file.