forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriveroverflows.py
More file actions
34 lines (28 loc) ยท 873 Bytes
/
riveroverflows.py
File metadata and controls
34 lines (28 loc) ยท 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import *
class Solution:
"""
ํ์ด:
s์ t ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด anagram ์๋๋ฏ๋ก False๋ก early return
set์ผ๋ก s์ ์ค๋ณต ๋ฌธ์ ์ ๊ฑฐ
set์ ์ํํ๋ฉด์ ๊ฐ ๋ฌธ์๊ฐ s์ t์์ ๋ฑ์ฅํ๋ ํ์๊ฐ ๋์ผํ์ง ํ์ธ
๋ค๋ฅด๋ฉด False, ๋๊น์ง ํต๊ณผํ๋ฉด True
TC: O(n+m)
- if len(s) != len(t): O(1)
- ss = set(s): O(n)
- for c in ss: ์ต๋ 26ํ ๋ฐ๋ณต โ O(1)
- s.count(c): O(n)
- t.count(c): O(m)
- ์ต์ข
: O(n+m)
SC: O(n)
- ss = set(s): ์ต์
์ ๊ฒฝ์ฐ O(n)
- ๊ทธ ์ธ ์์: O(1)
- ์ต์ข
: O(n)
"""
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
ss = set(s)
for c in ss:
if s.count(c) != t.count(c):
return False
return True