📌  相关文章
📜  检查是否可以对字符串进行置换,使其不包含长度为2的回文

📅  最后修改于: 2021-06-01 00:26:56             🧑  作者: Mango

给定字符串S的长度N ,仅由“ a”“ b”“ c”组成。任务是检查是否有可能置换S的字符,使其不包含长度为2或更大的回文作为子字符串。

例子:

Input: S = "abac"
Output: Yes
Explanation : 
1. The string contains a palindrome "aba". 
2. We can permute the last three characters as follows: S = "acba". 
3. Therefore, it does not contain any palindrome of length 2 or more. 

Input: S = "aba"
Output: No

方法:按照以下步骤解决问题:

  • 遍历字符串。
  • 对于长度为2的回文,两个字符应相同,即“ aa”“ bb”“ cc”
  • 类似地,对于长度为3的回文,相同的字母由另一个字母分隔。示例– “ a? a”“ b”? b”。
  • 因此,任何两个相同的字符必须至少由两个字符分隔。
  • 查找字符串中字符的频率。
  • 如果计数之差:
    • “ a”“ b”小于1
    • “ b”“ c”小于1
    • “ a”“ c”小于1
  • 如果所有三个条件都为真,则返回“是”
  • 否则返回“否”。

下面是上述方法的实现:

C++
// C++ implementation to print the character and
// its frequency in order of its occurrence
#include 
using namespace std;
 
void isPossible(string &str)
{
     
    //Find the frequency of the characters
    //in the string
    map mp;
    for(auto it : str){
        mp[it]++;
    }
     
    //Count of characters
    int x = mp['a'];
    int y = mp['b'];
    int z = mp['c'];
     
    //If satisfies the conditions
    if(abs(x-y) <= 1 and abs(y-z) <= 1 and abs(x-z) <= 1){
        cout << "Yes" << "\n";
    }
    //Return No
    else{
        cout << "No" << "\n";
    }
}
 
// Driver program to test above
int main()
{
    string str = "abac";
     
    isPossible(str);
     
    return 0;
}


Java
// Java implementation to print the
// character and its frequency in
// order of its occurrence
import java.io.*;
import java.util.*;
 
class GFG{
     
public static void isPossible(String str)
{
     
    // Find the frequency of the characters
    // in the string
    HashMap mp = new HashMap();
    for(int i = 0; i < str.length(); i++)
    {
        if (mp.containsKey(str.charAt(i)))
        {
            mp.put(str.charAt(i),
            mp.get(str.charAt(i)) + 1);
        }
        else
        {
            mp.put(str.charAt(i), 1);
        }
    }
     
    // Count of characters
    int x = mp.get('a');
    int y = mp.get('b');
    int z = mp.get('c');
      
    // If satisfies the conditions
    if (Math.abs(x - y)<= 1 &&
        Math.abs(y - z) <= 1 &&
        Math.abs(x - z) <= 1)
    {
        System.out.println("Yes");
    }
     
    // Return No
    else
    {
        System.out.println("No");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "abac";
     
    isPossible(str);
}
}
 
// This code is contributed by rag2127


Python3
# Python3 implementation to print the character and
# its frequency in order of its occurrence
def isPossible(Str) :
     
    # Find the frequency of the characters
    # in the string
    mp = {}
    for it in Str :
        if it in mp :
            mp[it] += 1
        else :
            mp[it] = 1
     
    # Count of characters
    x = mp['a']
    y = mp['b']
    z = mp['c']
     
    # If satisfies the conditions
    if(abs(x - y) <= 1 and abs(y - z) <= 1 and abs(x - z) <= 1) :
        print("Yes")
     
    # Return No
    else :
        print("No")
 
# Driver code
Str = "abac"
 
isPossible(Str)
 
# This code is contributed by divyesh072019


C#
// C# implementation to print the
// character and its frequency in
// order of its occurrence
using System;
using System.Collections.Generic;
 
class GFG{
     
static void isPossible(string str)
{
     
    // Find the frequency of the characters
    // in the string
    Dictionary mp = new Dictionary();
                                         
    foreach(char it in str)
    {
        if (mp.ContainsKey(it))
        {
            mp[it]++;
        }
        else
        {
            mp[it] = 1;
        }
    }
     
    // Count of characters
    int x = mp['a'];
    int y = mp['b'];
    int z = mp['c'];
      
    // If satisfies the conditions
    if (Math.Abs(x - y) <= 1 &&
        Math.Abs(y - z) <= 1 &&
        Math.Abs(x - z) <= 1)
    {
        Console.WriteLine("Yes");
    }
     
    // Return No
    else
    {
        Console.WriteLine("No");
    }
}
 
// Driver Code
static void Main()
{
    string str = "abac";
     
    isPossible(str);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:

Yes

时间复杂度: O(N ),其中N是字符串的长度

空间复杂度: O(N)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”