-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC1328-Break-a-Palindrome.py
77 lines (61 loc) · 2.18 KB
/
LC1328-Break-a-Palindrome.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Given a palindromic string of lowercase English letters palindrome,
replace exactly one character with any lowercase English letter so
that the resulting string is not a palindrome and that it is the
lexicographically smallest one possible.
Return the resulting string. If there is no way to replace a character
to make it not a palindrome, return an empty string.
A string a is lexicographically smaller than a string b (of the same
length) if in the first position where a and b differ, a has a
character strictly smaller than the corresponding character in b.
For example, "abcc" is lexicographically smaller than "abcd" because
the first position they differ is at the fourth character, and 'c'
is smaller than 'd'.
Example 1:
Input: palindrome = "abccba"
Output: "aaccba"
Explanation: There are many ways to make "abccba" not a palindrome,
such as "zbccba", "aaccba", and "abacba".
Of all the ways, "aaccba" is the lexicographically smallest.
Example 2:
Input: palindrome = "a"
Output: ""
Explanation: There is no way to replace a single character
to make "a" not a palindrome, so return an empty string.
Example 3:
Input: palindrome = "aa"
Output: "ab"
Example 4:
Input: palindrome = "aba"
Output: "abb"
Constraints:
1 <= palindrome.length <= 1000
palindrome consists of only lowercase English letters.
"""
class Solution:
def breakPalindrome(self, palindrome: str) -> str:
if len(palindrome) == 1:
return ''
# find first character which is not a
i = 0
while i < len(palindrome) and palindrome[i] == 'a':
i += 1
if i == len(palindrome):
return 'a' * (len(palindrome) - 1) + 'b'
if len(palindrome) % 2 == 1 and i == len(palindrome) // 2:
return palindrome[:-1] + 'b'
return palindrome[:i] + 'a' + palindrome[i + 1:]
if __name__ == "__main__":
from run_tests import run_tests
correct_answers = [
["abccba", "aaccba"],
["a", ""],
["aa", "ab"],
["cc", "ac"],
["aba", "abb"],
["cbc", "abc"],
["aaa", "aab"],
["bab", "aab"]
]
print(f"Running tests for breakPalindrome")
run_tests(Solution().breakPalindrome, correct_answers)