鉴于两个整数N和K,任务是找到字典序第K个最大的大小为N从仅包含两个字符“X”和“Y”一组字符串,其中字符“X”是存在的字符串中的字符串(N – 2) 次并且字符’y’ 只出现 2 次。
例子:
Input: N = 4, K = 3
Output: yxxy
Explanation:
All the strings of size 4 –
{ xxyy, xyxy, xyyx, yxxy, yxyx, yyxx }
The 3rd smallest string will be – yxxy
Input: N = 3, K = 2
Output: yxy
Explanation:
All the strings of size 3 –
{ xyy, yxy, yyx }
方法:
这个想法是观察字典序最大的字符串在开始时将有 2 个“y”,然后是所有的“x”。字典序第二大的字符串会将第二个 ‘y’ 向前移动一个索引,即 ‘yxyxxxxx……’ 等等。
这个问题的关键观察是字符’y’在字典序最大的字符串的前面出现两次,然后在每一步中,第二个字符’y’向前移动一步,直到它到达字符串的末尾以生成下一个最小的字符串。一旦第二个 ‘y’ 到达字符串 的末尾,下一个最小的字符串将在索引 1 和 2 处有两个 ‘y’,然后该过程继续。
因此,我们的想法是找到字符’y’ 的第一个和第二个位置,然后用 ‘y’字符打印这些位置,并用 ‘x’字符填充所有其他位置。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to print the
// kth largest string
void kthString(int n, int k)
{
int total = 0;
int i = 1;
// loop to iterate through
// series
while (total < k) {
// total takes the position
// of second y
total = total + n - i;
// i takes the position of
// first y
i++;
}
// calculating first y postion
int first_y_position = i - 1;
// calculating second y position
// from first y
int second_y_position = k - (total - n + first_y_position);
// print all x before first y
for (int j = 1; j < first_y_position; j++)
cout << "x";
// print first y
cout << "y";
int j = first_y_position + 1;
// print all x between first y
// and second y
while (second_y_position > 1) {
cout << "x";
second_y_position--;
j++;
}
// print second y
cout << "y";
// print x which occur
// after second y
while (j < n) {
cout << "x";
j++;
}
}
// Driver code
int main()
{
int n = 5;
int k = 7;
kthString(n, k);
return 0;
}
Java
// Java implementation of above approach
class GFG{
// Function to print the
// kth largest String
static void kthString(int n, int k)
{
int total = 0;
int i = 1;
// loop to iterate through
// series
while (total < k) {
// total takes the position
// of second y
total = total + n - i;
// i takes the position of
// first y
i++;
}
// calculating first y postion
int first_y_position = i - 1;
// calculating second y position
// from first y
int second_y_position = k - (total - n + first_y_position);
// print all x before first y
for (int j = 1; j < first_y_position; j++)
System.out.print("x");
// print first y
System.out.print("y");
int j = first_y_position + 1;
// print all x between first y
// and second y
while (second_y_position > 1) {
System.out.print("x");
second_y_position--;
j++;
}
// print second y
System.out.print("y");
// print x which occur
// after second y
while (j < n) {
System.out.print("x");
j++;
}
}
// Driver code
public static void main(String[] args)
{
int n = 5;
int k = 7;
kthString(n, k);
}
}
// This code is contributed by sapnasingh4991
Python 3
# Python 3 implementation of above approach
# Function to print the
# kth largest string
def kthString(n,k):
total = 0
i = 1
# loop to iterate through
# series
while (total < k):
# total takes the position
# of second y
total = total + n - i
# i takes the position of
# first y
i += 1
# calculating first y postion
first_y_position = i - 1
# calculating second y position
# from first y
second_y_position = k - (total - n + first_y_position)
# print all x before first y
for j in range(1,first_y_position,1):
print("x",end = "")
# print first y
print("y",end = "")
j = first_y_position + 1
# print all x between first y
# and second y
while (second_y_position > 1):
print("x",end = "")
second_y_position -= 1
j += 1
# print second y
print("y",end = "")
# print x which occur
# after second y
while (j < n):
print("x")
j += 1
# Driver code
if __name__ == '__main__':
n = 5
k = 7
kthString(n, k)
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of above approach
using System;
class GFG{
// Function to print the
// kth largest string
static void kthString(int n, int k)
{
int total = 0;
int i = 1;
// loop to iterate through
// series
while (total < k) {
// total takes the position
// of second y
total = total + n - i;
// i takes the position of
// first y
i++;
}
// calculating first y postion
int first_y_position = i - 1;
// calculating second y position
// from first y
int second_y_position = k - (total - n + first_y_position);
int j;
// print all x before first y
for (j = 1; j < first_y_position; j++)
Console.Write("x");
// print first y
Console.Write("y");
j = first_y_position + 1;
// print all x between first y
// and second y
while (second_y_position > 1) {
Console.Write("x");
second_y_position--;
j++;
}
// print second y
Console.Write("y");
// print x which occur
// after second y
while (j < n) {
Console.Write("x");
j++;
}
}
// Driver code
static public void Main ()
{
int n = 5;
int k = 7;
kthString(n, k);
}
}
// This code is contributed by shubhamsingh10
Javascript
xyxxy
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live