Skip to content

Commit 08c7733

Browse files
committed
Generate a String With Characters That Have Odd Counts
1 parent bd69104 commit 08c7733

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/
3+
4+
Given an integer n, return a string with n characters such that each character in such string occurs
5+
an odd number of times.
6+
The returned string must contain only lowercase English letters. If there are multiples valid strings,
7+
return any of them.
8+
9+
Example 1:
10+
Input: n = 4
11+
Output: "pppz"
12+
Explanation: "pppz" is a valid string since the character 'p' occurs three times and the
13+
character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
14+
15+
Example 2:
16+
Input: n = 2
17+
Output: "xy"
18+
Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once.
19+
Note that there are many other valid strings such as "ag" and "ur".
20+
21+
Example 3:
22+
Input: n = 7
23+
Output: "holasss"
24+
25+
Constraints:
26+
1 <= n <= 500
27+
"""
28+
class Solution:
29+
def generateTheString(self, n: int) -> str:
30+
if n % 2 != 0:
31+
return "a"*n
32+
return "a"*(n-1) + "b"

0 commit comments

Comments
 (0)