📌  相关文章
📜  生成一个由满足给定条件的字符“a”和“b”组成的字符串

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

生成一个由满足给定条件的字符“a”和“b”组成的字符串

给定两个整数AB ,任务是生成并打印一个字符串str ,使得:

  1. str只能包含字符'a''b'
  2. str的长度为A + B ,字符“a”的出现等于A ,字符“b”的出现等于B
  3. 子字符串“aaa”“bbb”不得出现在str中。

请注意,对于AB的给定值,始终可以生成有效的字符串。
例子:

方法:

  • 如果发生(a)>发生(b)然后附加“aab”
  • 如果发生(b)>发生(a)然后附加“bba”
  • 如果发生(a)=发生(b)然后附加“ab”

由于我们在每次迭代中将'a''b'的出现次数之间的差异最多减少 1 次,因此保证“bba”“aab”之后不会分别跟随“aab”“bba”
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to generate and print the required string
void generateString(int A, int B)
{
    string rt;
    while (0 < A || 0 < B) {
 
        // More 'b', append "bba"
        if (A < B) {
            if (0 < B--)
                rt.push_back('b');
            if (0 < B--)
                rt.push_back('b');
            if (0 < A--)
                rt.push_back('a');
        }
 
        // More 'a', append "aab"
        else if (B < A) {
            if (0 < A--)
                rt.push_back('a');
            if (0 < A--)
                rt.push_back('a');
            if (0 < B--)
                rt.push_back('b');
        }
 
        // Equal number of 'a' and 'b'
        // append "ab"
        else {
            if (0 < A--)
                rt.push_back('a');
            if (0 < B--)
                rt.push_back('b');
        }
    }
    cout << rt;
}
 
// Driver code
int main()
{
    int A = 2, B = 6;
    generateString(A, B);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function to generate and
    // print the required string
    static void generateString(int A, int B)
    {
        String rt = "";
        while (0 < A || 0 < B)
        {
 
            // More 'b', append "bba"
            if (A < B)
            {
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
            }
             
            // More 'a', append "aab"
            else if (B < A)
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
             
            // Equal number of 'a' and 'b'
            // append "ab"
            else
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
        }
        System.out.println(rt);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 2, B = 6;
        generateString(A, B);
    }
}
 
// This code is contributed
// by PrinciRaj1992


Python 3
# Python 3 implementation of the approach
 
# Function to generate and print
# the required string
def generateString(A, B):
 
    rt = ""
    while (0 < A or 0 < B) :
 
        # More 'b', append "bba"
        if (A < B) :
            if (0 < B):
                rt = rt +'b'
                B -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
            if (0 < A):
                rt += 'a'
                A -= 1
 
        # More 'a', append "aab"
        elif (B < A):
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
 
        # Equal number of 'a' and 'b'
        # append "ab"
        else :
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
    print(rt)
 
# Driver code
if __name__ == "__main__":
     
    A = 2
    B = 6
    generateString(A, B)
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to generate and
    // print the required string
    static void generateString(int A, int B)
    {
        string rt = "";
        while (0 < A || 0 < B)
        {
 
            // More 'b', append "bba"
            if (A < B)
            {
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
            }
             
            // More 'a', append "aab"
            else if (B < A)
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
             
            // Equal number of 'a' and 'b'
            // append "ab"
            else
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
        }
        Console.WriteLine(rt);
    }
 
    // Driver code
    public static void Main()
    {
        int A = 2, B = 6;
        generateString(A, B);
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
bbabbabb