获取重复子串形成的解码字符串的第K个字母
给定一个包含字母和数字的字符串S和一个整数K其中, 和 .任务是返回新字符串S'的第 K 个字母。
新字符串S'由旧字符串S通过以下步骤形成:
1. 如果读取的字符是字母,则该字母添加到S'的末尾。
2. 如果读取的字符是数字,则整个字符串S' 总共重复写入d-1次。
注意:新字符串保证少于2^63个字母。
例子:
Input: S = “geeks2for2”, K = 15
Output: “e”
Explanation: The new string S’ = “geeksgeeksforgeeksgeeksfor”. The 15th letter is “e”.
Input: S = “a2345”, K = 100
Output: “a”
Explanation: The new string S’=”a” repeated 120 times. The 100th letter is “a”.
让我们取一个像S' = “geeksgeeksgeeksgeeksgeeks”这样的新字符串和一个索引K = 22 ,那么如果K = 2 , K = 22的答案是相同的。
一般来说,当一个字符串等于某个大小长度重复一定次数的单词时(例如大小 = 5 重复 5 次的极客),那么索引K的答案与索引K %的答案相同大小。
使用这种洞察力并向后工作,我们跟踪新字符串S'的大小。每当字符串S'等于某个单词重复d次时,我们可以将K减少到K % (lengthof(word)) 。
我们首先找到新字符串S'的长度。之后,我们将向后工作,跟踪大小:解析符号S[0]、S[1]、...、S[i]后新字符串的长度。
如果我们看到一个数字S[i] ,则表示解析后新字符串的大小S[0], S[1], ..., S[i-1]将是(size / toInteger(S[i]) ) .否则,它将是size – 1 。
以下是上述方法的实现:
C++
// CPP implementation of above approach
#include
using namespace std;
// Function to return the K-th letter from new String.
string K_thletter(string S, int K)
{
int N = S.size();
long size = 0;
// finding size = length of new string S'
for (int i = 0; i < N; ++i) {
if (isdigit(S[i]))
size = size * (S[i] - '0');
else
size += 1;
}
// get the K-th letter
for (int i = N - 1; i >= 0; --i) {
K %= size;
if (K == 0 && isalpha(S[i]))
return (string) "" + S[i];
if (isdigit(S[i]))
size = size / (S[i] - '0');
else
size -= 1;
}
}
// Driver program
int main()
{
string S = "geeks2for2";
int K = 15;
cout << K_thletter(S, K);
return 0;
}
// This code is written by Sanjit_Prasad
Java
// Java implementation of above approach
class GFG
{
// Function to return the K-th letter from new String.
static String K_thletter(String S, int K)
{
int N = S.length();
long size = 0;
// finding size = length of new string S'
for (int i = 0; i < N; ++i)
{
if (Character.isDigit(S.charAt(i)))
{
size = size * (S.charAt(i) - '0');
}
else
{
size += 1;
}
}
// get the K-th letter
for (int i = N - 1; i >= 0; --i)
{
K %= size;
if (K == 0 && Character.isAlphabetic(S.charAt(i)))
{
return (String) "" + S.charAt(i);
}
if (Character.isDigit(S.charAt(i)))
{
size = size / (S.charAt(i) - '0');
}
else
{
size -= 1;
}
}
return null;
}
// Driver program
public static void main(String[] args)
{
String S = "geeks2for2";
int K = 15;
System.out.println(K_thletter(S, K));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of above approach
# Function to return the K-th letter
# from new String.
def K_thletter(S, K):
N = len(S)
size = 0
# finding size = length of new string S'
for i in range(0, N):
if S[i].isdigit():
size = size * int(S[i])
else:
size += 1
# get the K-th letter
for i in range(N - 1, -1, -1):
K %= size
if K == 0 and S[i].isalpha():
return S[i]
if S[i].isdigit():
size = size // int(S[i])
else:
size -= 1
# Driver Code
if __name__ == "__main__":
S = "geeks2for2"
K = 15
print(K_thletter(S, K))
# This code is contributed
# by Rituraj Jain
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the K-th letter from new String.
static String K_thletter(String S, int K)
{
int N = S.Length;
long size = 0;
// finding size = length of new string S'
for (int i = 0; i < N; ++i)
{
if (char.IsDigit(S[i]))
{
size = size * (S[i] - '0');
}
else
{
size += 1;
}
}
// get the K-th letter
for (int i = N - 1; i >= 0; --i)
{
K %= (int)size;
if (K == 0 && char.IsLetter(S[i]))
{
return (String) "" + S[i];
}
if (char.IsDigit(S[i]))
{
size = size / (S[i] - '0');
}
else
{
size -= 1;
}
}
return null;
}
// Driver code
public static void Main(String[] args)
{
String S = "geeks2for2";
int K = 15;
Console.WriteLine(K_thletter(S, K));
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
e
时间复杂度: O(N),其中 N 是 S 的长度。