forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1328.cpp
More file actions
43 lines (31 loc) · 1.62 KB
/
1328.cpp
File metadata and controls
43 lines (31 loc) · 1.62 KB
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
//1328. Break a Palindrome -- Medium
/*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.
*/
class Solution {
public:
string breakPalindrome(string palindrome) {
int n = palindrome.size();
//Not possible to make replacement when length is 0 or 1
if(n == 0 || n == 1)
return "";
for(int i=0; i<n; i++){
//Change the first non 'a' character to 'a'
if(palindrome[i] != 'a'){
if (!(n%2 != 0 && i == n/2))
return palindrome.substr(0, i) + 'a' + palindrome.substr(i+1);
}
}
//Change the last character to 'b'
return palindrome.substr(0, n-1) + 'b';
}
};
// This code is contributed by Nikhil-1503.