📌  相关文章
📜  检查一个字符串的字符是否可以交换形成另一个

📅  最后修改于: 2022-05-13 01:57:07.166000             🧑  作者: Mango

检查一个字符串的字符是否可以交换形成另一个

给出了两个字符串,我们需要通过交换第一个字符串的字符来判断是否可以形成第二个字符串。
例子:

Input : str1 = "geeksforgeeks" 
        str2 = "geegeeksksfor"
Output : YES

Input : str1 = "geeksfor"  
        str2 = "geeekfor"
Output : NO

首先,我们将找到字符串的长度,如果长度不相等,则意味着我们无法通过交换第一个字符串的字符来形成目标字符串。如果长度相等,那么我们遍历第一个字符串并为其创建一个映射,然后,如果映射中的任何索引为负数,我们将遍历第二个字符串并减少映射的计数,这意味着我们不能形成目标字符串,否则我们可以形成目标字符串。

Algorithm-
1- l1 = str1.length()  &&   l2 = str2.length()
2- if (l1 != l2)
    print "NO" 
3- Else
   map[26] = {0};
   for i=0 to l1
    map[str1[i]-'a']++;
   for i=0 to l2
     map[str2[i]-'a']--;
     if (map[str[2]-'a'<0)
      print "NO"
4- if no index goes negative print "YES"
5- End

C++
#include 
using namespace std;
const int MAX = 26;
 
bool targetstring(string str1, string str2)
{
    int l1 = str1.length();
    int l2 = str2.length();
 
    // if length is not same print no
    if (l1 != l2)
        return false;
 
    int map[MAX] = { 0 };
 
    // Count frequencies of character in
    // first string.
    for (int i = 0; i < l1; i++)
        map[str1[i] - 'a']++;
 
    // iterate through the second string
    // decrement counts of characters in
    // second string
    for (int i = 0; i < l2; i++) {
        map[str2[i] - 'a']--;
 
        // Since lengths are same, some
        // value would definitely become
        // negative if result is false.
        if (map[str2[i] - 'a'] < 0)
            return false;
    }
 
    return true;
}
 
// driver function
int main()
{
    string str1 = "geeksforgeeks";
    string str2 = "geegeeksksfor";
    if (targetstring(str1, str2))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java program to check if
// characters of one string
// can be swapped to form other
class GFG
{
static int MAX = 26;
 
static boolean targetstring(String str1,
                            String str2)
{
    int l1 = str1.length();
    int l2 = str2.length();
 
    // if length is not same print no
    if (l1 != l2)
        return false;
 
    int []map = new int[MAX];
     
    // Count frequencies of
    // character in first string.
    for (int i = 0; i < l1; i++)
        map[str1.charAt(i) - 'a']++;
 
    // iterate through the second
    // string decrement counts of
    // characters in second string
    for (int i = 0; i < l2; i++)
    {
        map[str2.charAt(i) - 'a']--;
 
        // Since lengths are same,
        // some value would definitely
        // become negative if result
        // is false.
        if (map[str2.charAt(i) - 'a'] < 0)
            return false;
    }
 
    return true;
}
 
// Driver Code
public static void main(String args[])
{
    String str1 = "geeksforgeeks";
    String str2 = "geegeeksksfor";
    if (targetstring(str1, str2))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by
// Akanksha Rai


Python3
# Python3 program to check if
# characters of one string
# can be swapped to form other
MAX = 26
 
def targetstring(str1, str2):
 
    l1 = len(str1)
    l2 = len(str2)
 
    # if length is not same print no
    if (l1 != l2):
        return False
 
    map = [0] * MAX
 
    # Count frequencies of character
    # in first string.
    for i in range (l1):
        map[ord(str1[i]) - ord('a')] += 1
 
    # iterate through the second string
    # decrement counts of characters in
    # second string
    for i in range(l2) :
        map[ord(str2[i]) - ord('a')] -= 1
 
        # Since lengths are same, some
        # value would definitely become
        # negative if result is false.
        if (map[ord(str2[i]) - ord('a')] < 0):
            return False
 
    return True
 
# Driver Code
if __name__ == "__main__":
 
    str1 = "geeksforgeeks"
    str2 = "geegeeksksfor"
    if (targetstring(str1, str2)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by ita_c


C#
// C# program to check if
// characters of one string
// can be swapped to form other
using System;
 
class GFG
{
    static int MAX = 26;
     
    static bool targetstring(string str1,
                             string str2)
    {
        int l1 = str1.Length;
        int l2 = str2.Length;
     
        // if length is not
        // same print no
        if (l1 != l2)
            return false;
     
        int []map = new int[MAX];
        Array.Clear(map, 0, 26);
     
        // Count frequencies of
        // character in first string.
        for (int i = 0; i < l1; i++)
            map[str1[i] - 'a']++;
     
        // iterate through the second
        // string decrement counts of
        // characters in second string
        for (int i = 0; i < l2; i++)
        {
            map[str2[i] - 'a']--;
     
            // Since lengths are same,
            // some value would definitely
            // become negative if result
            // is false.
            if (map[str2[i] - 'a'] < 0)
                return false;
        }
     
        return true;
    }
     
    // Driver Code
    static void Main()
    {
        string str1 = "geeksforgeeks";
        string str2 = "geegeeksksfor";
        if (targetstring(str1, str2))
            Console.Write("YES");
        else
            Console.Write("NO");
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP


Javascript


输出:
YES

时间复杂度- O(n)