📌  相关文章
📜  通过插入给定字符使字符串非回文

📅  最后修改于: 2021-09-04 09:35:42             🧑  作者: Mango

给定一个字符串S和一个字符X ,任务是通过在字符串S 中插入字符X来生成一个非回文字符串。如果无法获得非回文字符串,则打印“-1”

例子:

方法:根据以下观察可以解决给定的问题,如果字符串仅包含字符X ,则不可能使字符串非回文。否则,字符串可以是非回文的。请按照以下步骤解决问题:

  1. 如果在字符串S字符X的计数是相同的字符串S的长度,然后打印“-1”。
  2. 否则,检查字符串S和字符X 的连接是否为回文。如果发现为,则打印字符串S+X 。否则打印(X + S)

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if a
// string is palindromic or not
bool Palindrome(string str)
{
    // Traverse the string str
    for (int i = 0, j = str.length() - 1;
         i < j; i++, j--) {
 
        // Check if i-th character from
        // both ends are the same or not
        if (str[i] != str[j])
            return false;
    }
 
    // Return true, as str is palindrome
    return true;
}
 
// Function to make the non-palindromic
// string by inserting the character X
void NonPalindrome(string str, char X)
{
 
    // If all the characters
    // in the string are X
    if (count(str.begin(), str.end(), X)
        == str.length()) {
 
        cout << "-1";
        return;
    }
 
    // Check if X + str is
    // palindromic or not
    if (Palindrome(X + str))
        cout << str + X << endl;
    else
        cout << X + str << endl;
}
 
// Driver Code
int main()
{
    string S = "geek";
    char X = 's';
    NonPalindrome(S, X);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to check if a
    // string is palindromic or not
    static boolean Palindrome(String str)
    {
        // Traverse the string str
        for (int i = 0, j = str.length() - 1; i < j;
             i++, j--) {
 
            // Check if i-th character from
            // both ends are the same or not
            if (str.charAt(i) != str.charAt(j))
                return false;
        }
 
        // Return true, as str is palindrome
        return true;
    }
 
    // Function to make the non-palindromic
    // string by inserting the character X
    static void NonPalindrome(String str, char X)
    {
 
        // stores the count of char X in str
        int count = 0;
        for (int i = 0; i < str.length(); i++)
            if (str.charAt(i) == X)
                count++;
 
        // If all the characters
        // in the string are X
        if (count == str.length()) {
 
            System.out.println("-1");
            return;
        }
 
        // Check if X + str is
        // palindromic or not
        if (Palindrome(X + str))
            System.out.println(str + X);
        else
            System.out.println(X + str);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        String S = "geek";
        char X = 's';
        NonPalindrome(S, X);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to check if a
# string is palindromic or not
def Palindrome(str):
     
    if str == str[::-1]:
        return True
 
    # Return true, as str is palindrome
    return False
 
# Function to make the non-palindromic
# string by inserting the character X
def NonPalindrome(str, X):
 
    # If all the characters
    # in the string are X
    if (str.count(X) == len(str)):
        print("-1")
        return
 
    # Check if X + str is
    # palindromic or not
    if (Palindrome(X + str)):
        print(str + X)
    else:
        print(X + str)
 
# Driver Code
if __name__ == '__main__':
     
    S = "geek"
    X = 's'
     
    NonPalindrome(S, X)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
 
using System;
using System.Linq;
class GFG {
    // Function to check if a
    // string is palindromic or not
    static bool Palindrome(string str)
    {
        // Traverse the string str
        for (int i = 0, j = str.Length - 1; i < j;
             i++, j--) {
 
            // Check if i-th character from
            // both ends are the same or not
            if (str[i] != str[j])
                return false;
        }
 
        // Return true, as str is palindrome
        return true;
    }
 
    // Function to make the non-palindromic
    // string by inserting the character X
    static void NonPalindrome(string str, char X)
    {
 
        // If all the characters
        // in the string are X
        if (str.Count(p => p == X) == str.Length) {
 
            Console.Write("-1");
            return;
        }
 
        // Check if X + str is
        // palindromic or not
        if (Palindrome(X + str))
            Console.WriteLine(str + X);
        else
            Console.WriteLine(X + str);
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "geek";
        char X = 's';
        NonPalindrome(S, X);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:

sgeek

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live