给定一个只包含小写字母的回文字符串str ,任务是打印字典上最小的字符串,通过恰好替换一个字符,这样的字符串不是回文。
例子:
Input: str = “abccba”
Output: “aaccba”
Explanation:
Lexicographically smallest non-palindromic string possible is “aaccba”, here we have replaced the second letter ‘b’ by an ‘a’ which makes it a non palindrome.
Input: str = “a”
Output: -1
Explanation:
A single character is always a palindrome hence we cannot replace the value. Therefore the output is -1.
方法:为了解决这个问题上面提到,我们将检查只有一半的字符串和替换哪些不是所有的字符“a”到字符“a”本身。这个问题的边缘情况是如果只有一个字符,我们将返回一个空字符串。否则,如果所有字符都相同,那么我们将仅用字符’b’ 替换最后一个字符。
下面是上述方法的实现:
C++
// C++ program to make the string
// lexicographically smallest non
// palindromic string by replacing
// exactly one character
#include
using namespace std;
// Function to find the required string
string findStr(string S)
{
// length of the string
int n = S.size();
// Iterate till half of the string
for (int i = 0; i < n / 2; ++i) {
// replacing a non 'a' char with 'a'
if (S[i] != 'a') {
S[i] = 'a';
return S;
}
}
// Check if there is no 'a' in string
// we replace last char of string by 'b'
S[n - 1] = 'b';
// If the input is a single character
return n < 2 ? " -1 " : S;
}
// Driver code
int main()
{
string str = "a";
cout << findStr(str) << endl;
string str1 = "abccba";
cout << findStr(str1) << endl;
return 0;
}
Java
// Java program to make the string
// lexicographically smallest non
// palindromic string by replacing
// exactly one character
import java.util.*;
class GFG {
// Function to find the required string
static String findStr(String S)
{
StringBuilder sb = new StringBuilder(S);
// length of the string
int n = sb.length();
// Iterate till half of the string
for (int i = 0; i < n / 2; ++i)
{
// replacing a non 'a' char with 'a'
if (sb.charAt(i) != 'a')
{
sb.setCharAt(i, 'a');
return sb.toString();
}
}
// Check if there is no 'a' in string
// we replace last char of string by 'b'
sb.setCharAt(n - 1, 'b');
// If the input is a single character
return n < 2 ? " -1 " : sb.toString();
}
// Driver code
public static void main(String[] args)
{
String str = "a";
System.out.println(findStr(str));
String str1 = "abccba";
System.out.println(findStr(str1));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to make the string
# lexicographically smallest non
# palindromic string by replacing
# exactly one character
# Function to find the required string
def findStr(S):
S = list(S)
# Length of the string
n = len(S)
# Iterate till half of the string
for i in range(0, n // 2):
# Replacing a non 'a' char with 'a'
if S[i] != 'a':
S[i] = 'a'
return (''.join(S))
# Check if there is no 'a' in string
# we replace last char of string by 'b'
S[n - 1] = 'b'
# If the input is a single character
if n < 2:
return '-1'
else:
return (''.join(S))
# Driver Code
if __name__=='__main__':
str1 = 'a'
print(findStr(str1))
str2 = 'abccba'
print(findStr(str2))
# This code is contributed by rutvik_56
C#
// C# program to make the string
// lexicographically smallest non
// palindromic string by replacing
// exactly one character
using System;
class GFG{
// Function to find the required string
static String findStr(char []S)
{
// Length of the string
int n = S.Length;
// Iterate till half of the string
for(int i = 0; i < n / 2; ++i)
{
// Replacing a non 'a' char with 'a'
if (S[i] != 'a')
{
S[i] = 'a';
return new String(S);
}
}
// Check if there is no 'a' in string
// we replace last char of string by 'b'
S[n - 1] = 'b';
// If the input is a single character
return n < 2 ? " -1 " : new String(S);
}
// Driver code
public static void Main(String[] args)
{
String str = "a";
Console.WriteLine(findStr(str.ToCharArray()));
String str1 = "abccba";
Console.WriteLine(findStr(str1.ToCharArray()));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
-1
aaccba
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live