Skip to content

Commit bb6b443

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (37.85%)
1 parent 1e0bd9a commit bb6b443

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given a string <code>s</code>, return <code>true</code> <em>if a permutation of the string could form a </em><span data-keyword="palindrome-string"><em><strong>palindrome</strong></em></span><em> and </em><code>false</code><em> otherwise</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> s = &quot;code&quot;
8+
<strong>Output:</strong> false
9+
</pre>
10+
11+
<p><strong class="example">Example 2:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> s = &quot;aab&quot;
15+
<strong>Output:</strong> true
16+
</pre>
17+
18+
<p><strong class="example">Example 3:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> s = &quot;carerac&quot;
22+
<strong>Output:</strong> true
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= s.length &lt;= 5000</code></li>
30+
<li><code>s</code> consists of only lowercase English letters.</li>
31+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Approach 2: HashMap
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def canPermutePalindrome(self, s: str) -> bool:
10+
count = Counter(s)
11+
odds = sum(val % 2 for val in count.values())
12+
return odds <= 1
13+

0 commit comments

Comments
 (0)