📌  相关文章
📜  检查是否可以在给定约束下将一个字符串转换为另一个字符串

📅  最后修改于: 2021-06-25 23:41:09             🧑  作者: Mango

给定的两个字符串包含三个字符,即“ A”,“ B”和“#”。通过首先对字符串执行以下操作,检查是否可以将第一个字符串转换为另一个字符串。
1-‘A’只能向左移动
2-‘B’只能向右移动
3-‘A’和’B’都不交叉
如果可能,则打印“是”,否则打印“否”。
例子:

方法 :
两个字符串的1长度必须相同
2号两个字符串中A和B的个数必须相等
两个字符串中A和B的3阶应该相同(例如:如果第二个字符串“ A”在“ B”之前,那么必须首先对字符串进行相同的排序)

C++
// C++ Program for above implementation
#include 
using namespace std;
 
// Function to check is it possible to convert
// first string into another string or not.
bool isItPossible(string str1, string str2, int m, int n)
{
 
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
 
    // To Check  Frequency of A's and  B's are
    // equal in both strings or not.
    if (count(str1.begin(), str1.end(), 'A') !=
           count(str2.begin(), str2.end(), 'A') ||
        count(str1.begin(), str1.end(), 'B') !=
            count(str2.begin(), str2.end(), 'B'))
        return false;
 
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
 
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
 
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
 
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
 
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    if (str1[i] == 'B' && i > j)
                        return false;
 
                    break;
                }
            }
        }
    }
 
    return true;
}
 
// Drivers code
int main()
{
    string str1 = "A#B#";
    string str2 = "A##B";
 
    int m = str1.length();
    int n = str2.length();
 
    isItPossible(str1, str2, m, n) ? cout << "Yes\n"
                                   : cout << "No\n";
 
    return 0;
}


Java
// Java Program for above implementation
class GFG
{
 
// Function to check is it possible to convert
// first String into another String or not.
static boolean isItPossible(char[] str1, char[] str2,
                            int m, int n)
{
 
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
 
    // To Check Frequency of A's and B's are
    // equal in both Strings or not.
    if (count(str1, 'A') !=
        count(str2, 'A') ||
        count(str1, 'B') !=
            count(str2, 'B'))
        return false;
 
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
 
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
 
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
 
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
 
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    if (str1[i] == 'B' && i > j)
                        return false;
 
                    break;
                }
            }
        }
    }
 
    return true;
}
 
private static int count(char[] str1, char c) {
    int count = 0;
    for(char temp : str1) {
        if(c == temp)
            count++;
    }
    return count;
}
 
// Drivers code
public static void main(String[] args)
{
    String str1 = "A#B#";
    String str2 = "A##B";
 
    int m = str1.length();
    int n = str2.length();
 
    System.out.print(isItPossible(str1.toCharArray(), str2.toCharArray(), m, n) ?
            "Yes\n":"No\n");
 
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python Program for above implementation
 
# Function to check is it possible to convert
# first string into another string or not.
def isItPossible(str1, str2, m, n):
 
    # To Check Length of Both String is Equal or Not
    if (m != n):
        return False
 
    # To Check Frequency of A's and B's are
    # equal in both strings or not.
    if str1.count('A') != str2.count('A') \
    or str1.count('B') != str2.count('B'):
        return False
 
    # Start traversing
    for i in range(m):
        if (str1[i] != '#'):
            for j in range(n):
                # To Check no two elements cross each other.
                if ((str2[j] != str1[i]) and str2[j] != '#'):
                    return False
 
                if (str2[j] == str1[i]):
                    str2[j] = '#'
 
                    # To Check Is it Possible to Move
                    # towards Left or not.
                    if (str1[i] == 'A' and i < j):
                        return False
 
                    # To Check Is it Possible to Move
                    # towards Right or not.
                    if (str1[i] == 'B' and i > j):
                        return False
 
                    break
                 
    return True
 
# Drivers code
 
str1 = "A#B#"
str2 = "A##B"
 
m = len(str1)
n = len(str2)
 
str1 = list(str1)
str2 = list(str2)
 
if(isItPossible(str1, str2, m, n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by ankush_953


C#
// C# Program for above implementation
using System;
 
class GFG
{
  
// Function to check is it possible to convert
// first String into another String or not.
static bool isItPossible(char[] str1, char[] str2,
                            int m, int n)
{
  
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
  
    // To Check Frequency of A's and B's are
    // equal in both Strings or not.
    if (count(str1, 'A') !=
        count(str2, 'A') ||
        count(str1, 'B') !=
            count(str2, 'B'))
        return false;
  
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
  
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
  
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
  
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
  
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    if (str1[i] == 'B' && i > j)
                        return false;
  
                    break;
                }
            }
        }
    }
  
    return true;
}
  
private static int count(char[] str1, char c) {
    int count = 0;
    foreach(char temp in str1) {
        if(c == temp)
            count++;
    }
    return count;
}
  
// Drivers code
public static void Main(String[] args)
{
    String str1 = "A#B#";
    String str2 = "A##B";
  
    int m = str1.Length;
    int n = str2.Length;
  
    Console.Write(isItPossible(str1.ToCharArray(), str2.ToCharArray(), m, n) ?
            "Yes\n":"No\n");
  
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

Yes

时间复杂度: O(n ^ 2)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。