📌  相关文章
📜  计算交替放置两个给定字符串的所有字符的方法

📅  最后修改于: 2021-04-24 12:13:59             🧑  作者: Mango

给定两个字符串,长度为N str1和明显不同的字符长度MSTR2,任务是统计的方式发出str1str2的交替的所有字符数。

注意: | N – M | ≤1

例子:

方法:可以根据以下观察结果解决问题:

如果N!= M:仅考虑N> M的情况,因为类似地,它将适用于N 的情况。

交替放置str1 []str2 []的可能方法

如果N == M:

首先STR1 [],然后的字符放置STR2的字符[]

首先STR2 [],然后的字符放置STR1的字符[]

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get the
// factorial of N
int fact(int n)
{
    int res = 1;
    for (int i = 1; i <= n; i++) {
        res = res * i;
    }
    return res;
}
 
// Function to get the total
// number of  distinct ways
int distinctWays(string str1, string str2)
{
    // Length of str1
    int n = str1.length();
 
    // Length of str2
    int m = str2.length();
 
    // If both strings have equal length
    if (n == m) {
        return 2 * fact(n) * fact(m);
    }
 
    // If both strings do not have
    // equal length
    return fact(n) * fact(m);
}
 
// Driver code
int main()
{
    string str1 = "aegh";
    string str2 = "rsw";
 
    cout << distinctWays(str1, str2);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to get the
// factorial of N
static int fact(int n)
{
    int res = 1;
    for(int i = 1; i <= n; i++)
    {
        res = res * i;
    }
    return res;
}
 
// Function to get the total
// number of distinct ways
static int distinctWays(String str1,
                        String str2)
{
     
    // Length of str1
    int n = str1.length();
 
    // Length of str2
    int m = str2.length();
 
    // If both strings have equal length
    if (n == m)
    {
        return 2 * fact(n) * fact(m);
    }
 
    // If both strings do not have
    // equal length
    return fact(n) * fact(m);
}
 
// Driver code
public static void main (String[] args)
{
    String str1 = "aegh";
    String str2 = "rsw";
 
    System.out.print(distinctWays(str1, str2));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to get the
# factorial of N
def fact(n):
     
    res = 1
    for i in range(1, n + 1):
        res = res * i
     
    return res
 
# Function to get the total
# number of distinct ways
def distinctWays(str1, str2):
     
    # Length of str1
    n = len(str1)
 
    # Length of str2
    m = len(str2)
 
    # If both strings have equal length
    if (n == m):
        return 2 * fact(n) * fact(m)
     
    # If both strings do not have
    # equal length
    return fact(n) * fact(m)
 
# Driver code
str1 = "aegh"
str2 = "rsw"
 
print(distinctWays(str1, str2))
 
# This code is contributed by code_hunt


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to get the
// factorial of N
static int fact(int n)
{
    int res = 1;
    for(int i = 1; i <= n; i++)
    {
        res = res * i;
    }
    return res;
}
 
// Function to get the total
// number of distinct ways
static int distinctWays(string str1,
                        string str2)
{
     
    // Length of str1
    int n = str1.Length;
 
    // Length of str2
    int m = str2.Length;
 
    // If both strings have equal length
    if (n == m)
    {
        return 2 * fact(n) * fact(m);
    }
 
    // If both strings do not have
    // equal length
    return fact(n) * fact(m);
}
 
// Driver code
public static void Main ()
{
    string str1 = "aegh";
    string str2 = "rsw";
 
    Console.Write(distinctWays(str1, str2));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
144

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