Skip to content

Haktoberfest-2022 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Break_a_Palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Leat code problem of day (date 10/10/2022)
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'.


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.

"""


# Problem solution :

class Solution:
def breakPalindrome(self, palindrome: str) -> str:
if len(palindrome)==1:
return ""
cmp=palindrome
for i in range(len(palindrome)):
if palindrome[i] !="a":
palindrome=palindrome.replace(palindrome[i],"a",1)
if palindrome!=palindrome[::-1]:
return palindrome
else:
palindrome=cmp

palindrome=palindrome[:-1]
return palindrome+"b"