File tree Expand file tree Collapse file tree 1 file changed +35
-3
lines changed Expand file tree Collapse file tree 1 file changed +35
-3
lines changed Original file line number Diff line number Diff line change 12
12
13
13
See:
14
14
15
- Time Spent: min
16
15
"""
17
- import collections
18
- from typing import List
16
+ from tool import *
19
17
20
18
21
19
class Solution :
22
20
def longestPalindrome (self , words : List [str ]) -> int :
21
+ """
22
+ 2022/11/3
23
+ Runtime: 1657 ms, faster than 81.22%
24
+ Memory Usage: 38.3 MB, less than 86.56%
25
+
26
+ 1 <= words.length <= 10^5
27
+ words[i].length == 2
28
+ words[i] consists of lowercase English letters.
29
+ """
30
+ cnt = collections .Counter (words )
31
+ seen = set ()
32
+ word_set = set (words )
33
+
34
+ ret = 0
35
+ for word in word_set :
36
+ if word in seen :
37
+ continue
38
+ word2 = word [::- 1 ]
39
+ seen .add (word )
40
+ if word == word2 :
41
+ l = cnt [word ] // 2
42
+ ret += l * 4
43
+ elif word2 in word_set :
44
+ seen .add (word2 )
45
+ l = min (cnt [word ], cnt [word2 ])
46
+ ret += l * 4
47
+ for c in string .ascii_lowercase :
48
+ w = c * 2
49
+ if w in cnt and cnt [w ] & 1 :
50
+ ret += 2
51
+ break
52
+ return ret
53
+
54
+ def longestPalindrome2 (self , words : List [str ]) -> int :
23
55
"""
24
56
1 <= words.length <= 10^5
25
57
words[i].length == 2
You can’t perform that action at this time.
0 commit comments