📌  相关文章
📜  生成具有奇数频率的最大可能字母的字符串

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

给定一个整数N ,任务是生成一个字符串str ,其中包含最大可能的小写字母,每个字母出现奇数次。
例子:

方法:

  • 如果N小于等于 26,我们用N 个不同的字符填充字符串,每个字符出现一次。
  • 除此以外:
    • 如果N奇数,我们将 ‘b’-‘y’ 中的所有 24 个字符相加一次,并用 ‘a’ 填充剩余的奇数长度。
    • 如果N偶数,我们将 ‘b’-‘z’ 中的所有 25 个字符相加一次,并用 ‘a’ 填充剩余的奇数长度。

下面是上述方法的实现:

C++
// C++ program to generate a string
// of length n with maximum possible
// alphabets with each of them
// occuring odd number of times.
 
#include 
using namespace std;
 
// Function to generate a string
// of length n with maximum possible
// alphabets each occuring odd
// number of times.
string generateTheString(int n)
{
    string ans="";
    // If n is odd
    if(n%2)
    {
        // Add all characters from
        // b-y
        for(int i=0;i24)
        {
            for(int i=0;i<(n-24);i++)
                ans+='a';
        }
    }
    // If n is even
    else
    {
        // Add all characters from
        // b-z
        for(int i=0;i25)
        {
            for(int i=0;i<(n-25);i++)
                ans+='a';
        }
    }
     
    return ans;
}
 
// Driven code
int main()
{
    int n = 34;
    cout << generateTheString(n);
    return 0;
}


Java
// Java program to generate a string
// of length n with maximum possible
// alphabets with each of them
// occuring odd number of times.
import java.util.*;
 
class GFG{
     
// Function to generate a string
// of length n with maximum possible
// alphabets each occuring odd
// number of times.
static String generateTheString(int n)
{
    String ans = "";
     
    // If n is odd
    if (n % 2 != 0)
    {
 
        // Add all characters from
        // b-y
        for(int i = 0;
                i < Math.min(n, 24); i++)
        {
            ans += (char)('b' + i);
        }
         
        // Append a to fill the
        // remaining length
        if (n > 24)
        {
            for(int i = 0;
                    i < (n - 24); i++)
                ans += 'a';
        }
    }
     
    // If n is even
    else
    {
         
        // Add all characters from
        // b-z
        for(int i = 0;
                i < Math.min(n, 25); i++)
        {
            ans += (char)('b' + i);
        }
 
        // Append a to fill the
        // remaining length
        if (n > 25)
        {
            for(int i = 0;
                    i < (n - 25); i++)
                ans += 'a';
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 34;
     
    System.out.println(generateTheString(n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to generate a string
# of length n with maximum possible
# alphabets with each of them
# occuring odd number of times.
 
# Function to generate a string
# of length n with maximum possible
# alphabets each occuring odd
# number of times.
def generateTheString( n):
 
    ans = ""
     
    # If n is odd
    if(n % 2):
         
        # Add all characters from
        # b-y
        for i in range(min(n, 24)):
            ans += chr(ord('b') + i)
         
        # Append a to fill the
        # remaining length
        if(n > 24):
            for i in range((n - 24)):
                ans += 'a'
         
    # If n is even
    else:
         
        # Add all characters from
        # b-z
        for i in range(min(n, 25)):
            ans += chr(ord('b') + i)
         
        # Append a to fill the
        # remaining length
        if(n > 25):
            for i in range((n - 25)):
                ans += 'a'
    return ans
 
# Driver code
if __name__ == "__main__":
     
    n = 34
    print(generateTheString(n))
 
# This code is contributed by chitranayal


C#
// C# program to generate a string
// of length n with maximum possible
// alphabets with each of them
// occuring odd number of times.
using System;
class GFG{
 
// Function to generate a string
// of length n with maximum possible
// alphabets each occuring odd
// number of times.
static string generateTheString(int n)
{
    string ans = "";
     
    // If n is odd
    if(n % 2 == 0)
    {
        // Add all characters from
        // b-y
        for(int i = 0; i < Math.Min(n, 24); i++)
        {
            ans += (char)('b' + i);
        }
         
        // Append a to fill the
        // remaining length
        if(n > 24)
        {
            for(int i = 0; i < (n - 24); i++)
                ans += 'a';
        }
    }
     
    // If n is even
    else
    {
        // Add all characters from
        // b-z
        for(int i = 0; i < Math.Min(n, 25); i++)
        {
            ans += (char)('b' + i);
        }
         
        // Append a to fill the
        // remaining length
        if(n > 25)
        {
            for(int i = 0; i < (n - 25); i++)
                ans += 'a';
        }
    }
    return ans;
}
 
// Driven code
public static void Main()
{
    int n = 34;
    Console.Write(generateTheString(n));
}
}
 
// This code is contributed by Nidhi_Biet


Javascript


输出:
bcdefghijklmnopqrstuvwxyzaaaaaaaaa

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live