给定一个由 [1, 9] 中的N 个数字和一个正整数K和X组成的字符串S 。每天,字符串的每个字符都被替换为它的频率和值。任务是在X天后找到字符串的第 K 个字符。
例子:
Input: S = “1214”, K = 10, X = 3
Output: 4
Explanation:
1st day = “12214444”
2nd day = “1222214444444444444444”
3rd day = “122222222444444444444444444444444444444444444444444444444”
So, 10th character after 3rd day is 4.
Input: S =”123″, K = 6, X = 2
Output: 3
朴素的方法:解决问题的最简单的方法是通过将字符串digit 中的数字附加X次来创建字符串,并找到字符串的第 K 个字符。
时间复杂度: O(∑digit[i] X ) for i in range [0, N-1]
有效的方法:上述方法可以进一步优化,而不是创建一个字符串,将数字X添加到总和并检查sum > K 。请按照以下步骤解决问题:
- 初始化一个变量,比如sum ,它存储X天后字符串的字符总和。
- 初始化一个变量,比如在X天后存储第K 个字符的ans 。
- 在[0, N-1]范围内迭代并执行以下步骤:
- 将变量 say range初始化为(S[i] – ‘0’) X并将其添加到变量sum 中。
- 如果sum>=K ,则返回S ,将ans修改为S[i] ,并终止循环。
- 打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Kth character
// after X days
char FindKthChar(string str, long long K, int X)
{
// Variable to store the KthChar
char ans;
int sum = 0;
// Traverse the string
for (int i = 0; i < str.length(); i++) {
// Convert char into int
int digit = str[i] - '0';
// Calculate characters
int range = pow(digit, X);
sum += range;
// If K is less than sum
// than ans = str[i]
if (K <= sum) {
ans = str[i];
break;
}
}
// Return answer
return ans;
}
// Driver Code
int main()
{
// Given Input
string str = "123";
long long K = 9;
int X = 3;
// Function Call
char ans = FindKthChar(str, K, X);
cout << ans << "\n";
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the Kth character
// after X days
static char FindKthChar(String str, int K, int X)
{
// Variable to store the KthChar
char ans = ' ';
int sum = 0;
// Traverse the string
for(int i = 0; i < str.length(); i++)
{
// Convert char into int
int digit = (int)str.charAt(i) - 48;
// Calculate characters
int range = (int)Math.pow(digit, X);
sum += range;
// If K is less than sum
// than ans = str[i]
if (K <= sum)
{
ans = str.charAt(i);
break;
}
}
// Return answer
return ans;
}
// Driver code
public static void main(String[] args)
{
// Given Input
String str = "123";
int K = 9;
int X = 3;
// Function Call
char ans = FindKthChar(str, K, X);
System.out.println(ans);
}
}
// This code is contributed by abhinavjain194
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the Kth character
// after X days
static char FindKthChar(string str, int K, int X)
{
// Variable to store the KthChar
char ans = ' ';
int sum = 0;
// Traverse the string
for(int i = 0; i < str.Length; i++)
{
// Convert char into int
int digit = (int)str[i] - 48;
// Calculate characters
int range = (int)Math.Pow(digit, X);
sum += range;
// If K is less than sum
// than ans = str[i]
if (K <= sum)
{
ans = str[i];
break;
}
}
// Return answer
return ans;
}
// Driver Code
public static void Main()
{
// Given Input
string str = "123";
int K = 9;
int X = 3;
// Function Call
char ans = FindKthChar(str, K, X);
Console.Write(ans);
}
}
// This code is contributed by SURENDRA_GANGWAR
输出
2
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。