File tree Expand file tree Collapse file tree 5 files changed +100
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
encode-and-decode-strings Expand file tree Collapse file tree 5 files changed +100
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Codec :
5+ def encode (self , strs : List [str ]) -> str :
6+ """Encodes a list of strings to a single string."""
7+ encoded = []
8+
9+ for s in strs :
10+ encoded .append (s .replace ("/" , "//" ) + "/:" )
11+
12+ return "" .join (encoded )
13+
14+ def decode (self , s : str ) -> List [str ]:
15+ """Decodes a single string to a list of strings."""
16+ decoded = []
17+ current_string = ""
18+ i = 0
19+
20+ while i < len (s ):
21+ if s [i : i + 2 ] == "/:" :
22+ decoded .append (current_string )
23+ current_string = ""
24+ i += 2
25+ elif s [i : i + 2 ] == "//" :
26+ current_string += "/"
27+ i += 2
28+ else :
29+ current_string += s [i ]
30+ i += 1
31+
32+ return decoded
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def countBits (self , n : int ) -> List [int ]:
6+ answer = [0 ]
7+
8+ for i in range (1 , n + 1 ):
9+ answer .append (answer [i // 2 ] + i % 2 )
10+
11+ return answer
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numDecodings (self , s : str ) -> int :
3+ if s [0 ] == "0" :
4+ return 0
5+
6+ n = len (s )
7+ dp = [0 ] * (n + 1 )
8+ dp [0 ], dp [1 ] = 1 , 1
9+
10+ for i in range (2 , n + 1 ):
11+ if s [i - 1 ] != "0" :
12+ dp [i ] += dp [i - 1 ]
13+ if "10" <= s [i - 2 : i ] <= "26" :
14+ dp [i ] += dp [i - 2 ]
15+
16+ return dp [n ]
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Codec :
5+ def encode (self , strs : List [str ]) -> str :
6+ """Encodes a list of strings to a single string."""
7+ encoded = []
8+
9+ for s in strs :
10+ encoded .append (s .replace ("/" , "//" ) + "/:" )
11+
12+ return "" .join (encoded )
13+
14+ def decode (self , s : str ) -> List [str ]:
15+ """Decodes a single string to a list of strings."""
16+ decoded = []
17+ current_string = ""
18+ i = 0
19+
20+ while i < len (s ):
21+ if s [i : i + 2 ] == "/:" :
22+ decoded .append (current_string )
23+ current_string = ""
24+ i += 2
25+ elif s [i : i + 2 ] == "//" :
26+ current_string += "/"
27+ i += 2
28+ else :
29+ current_string += s [i ]
30+ i += 1
31+
32+ return decoded
Original file line number Diff line number Diff line change 1+ from collections import Counter
2+
3+
4+ class Solution :
5+ def isAnagram (self , s : str , t : str ) -> bool :
6+ if len (s ) != len (t ):
7+ return False
8+
9+ return Counter (s ) == Counter (t )
You can’t perform that action at this time.
0 commit comments