查找解密字符串的第 k 个字符|套装 – 2
给定一个编码字符串,其中子字符串的重复表示为子字符串,后跟子字符串的计数。例如,如果加密字符串是“ab2cd2”并且k=4,那么输出将是“b”,因为解密后的字符串是“ababcdcd”并且第4个字符是“b”。
注意:加密子串的频率可以超过一位。例如,在“ab12c3”中,ab 重复了 12 次。子串的频率中不存在前导 0。
例子:
Input: "a2b2c3", k = 5
Output: c
Decrypted string is "aabbccc"
Input: "ab4c2ed3", k = 9
Output : c
Decrypted string is "ababababccededed"
上一篇文章中讨论的解决方案需要额外的 O(n) 空间。以下帖子讨论了需要恒定空间的解决方案。逐步算法是:
- 查找当前子字符串的长度。使用两个指针。将一个指针固定在子字符串的开头并移动另一个指针,直到找不到数字为止。
- 通过进一步移动第二个指针直到找不到字母来查找重复频率。
- 如果通过将频率乘以它的原始长度来查找子字符串的长度。
- 如果此长度小于 k,则所需字符位于后面的子字符串中。从 k 中减去此长度以计算需要覆盖的字符数。
- 如果长度小于或等于 k,则所需字符位于当前子字符串中。由于 k 是 1-indexed ,因此将其减 1,然后将其 mod 与原始子字符串长度。所需字符是从第一个指针指向的子字符串开始的第 k 个字符。
下面是上述方法的实现:
C++
// C++ program to find K'th character in
// decrypted string
#include
using namespace std;
// Function to find K'th character in
// Encoded String
char encodedChar(string str, int k)
{
int i, j;
int n = str.length();
// To store length of substring
int len;
// To store length of substring when
// it is repeated
int num;
// To store frequency of substring
int freq;
i = 0;
while (i < n) {
j = i;
len = 0;
freq = 0;
// Find length of substring by
// traversing the string until
// no digit is found.
while (j < n && isalpha(str[j])) {
j++;
len++;
}
// Find frequency of preceding substring.
while (j < n && isdigit(str[j])) {
freq = freq * 10 + (str[j] - '0');
j++;
}
// Find length of substring when
// it is repeated.
num = freq * len;
// If length of repeated substring is less than
// k then required character is present in next
// substring. Subtract length of repeated
// substring from k to keep account of number of
// characters required to be visited.
if (k > num) {
k -= num;
i = j;
}
// If length of repeated substring is
// more or equal to k then required
// character lies in current substring.
else {
k--;
k %= len;
return str[i + k];
}
}
// This is for the case when there
// are no repetition in string.
// e.g. str="abced".
return str[k - 1];
}
// Driver Code
int main()
{
string str = "abced";
int k = 4;
cout << encodedChar(str, k) << endl;
return 0;
}
Java
// Java program to find K'th character in
// decrypted string
import java.util.*;
class GFG
{
// Function to find K'th character in
// Encoded String
static char encodedChar(char[] str, int k)
{
int i, j;
int n = str.length;
// To store length of substring
int len;
// To store length of substring when
// it is repeated
int num;
// To store frequency of substring
int freq;
i = 0;
while (i < n)
{
j = i;
len = 0;
freq = 0;
// Find length of substring by
// traversing the string until
// no digit is found.
while (j < n && Character.isAlphabetic(str[j]))
{
j++;
len++;
}
// Find frequency of preceding substring.
while (j < n && Character.isDigit(str[j]))
{
freq = freq * 10 + (str[j] - '0');
j++;
}
// Find length of substring when
// it is repeated.
num = freq * len;
// If length of repeated substring is less than
// k then required character is present in next
// substring. Subtract length of repeated
// substring from k to keep account of number of
// characters required to be visited.
if (k > num)
{
k -= num;
i = j;
}
// If length of repeated substring is
// more or equal to k then required
// character lies in current substring.
else
{
k--;
k %= len;
return str[i + k];
}
}
// This is for the case when there
// are no repetition in string.
// e.g. str="abced".
return str[k - 1];
}
// Driver Code
public static void main(String[] args)
{
String str = "abced";
int k = 4;
System.out.println(encodedChar(str.toCharArray(), k));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find K'th
# character in decrypted string
# Function to find K'th character
# in Encoded String
def encodedChar(string, k):
n = len(string)
i = 0
while i < n:
j = i
length = 0
freq = 0
# Find length of substring by
# traversing the string until
# no digit is found.
while j < n and string[j].isalpha():
j += 1
length += 1
# Find frequency of preceding substring.
while j < n and string[j].isdigit():
freq = freq * 10 + int(string[j])
j += 1
# Find the length of the substring
# when it is repeated.
num = freq * length
# If the length of the repeated substring
# is less than k then required character
# is present in next substring. Subtract
# the length of repeated substring from
# k to keep account of the number
# of characters required to be visited.
if k > num:
k -= num
i = j
# If length of repeated substring is
# more or equal to k then required
# character lies in current substring.
else:
k -= 1
k %= length
return string[i + k]
# This is for the case when there are no
# repetition in string. e.g. str="abced".
return string[k - 1]
# Driver Code
if __name__ == "__main__":
string = "abced"
k = 4
print(encodedChar(string, k))
# This code is contributed
# by Rituraj Jain
C#
// C# program to find K'th character in
// decrypted string
using System;
class GFG
{
// Function to find K'th character in
// Encoded String
static char encodedChar(char[] str, int k)
{
int i, j;
int n = str.Length;
// To store length of substring
int len;
// To store length of substring when
// it is repeated
int num;
// To store frequency of substring
int freq;
i = 0;
while (i < n)
{
j = i;
len = 0;
freq = 0;
// Find length of substring by
// traversing the string until
// no digit is found.
while (j < n && char.IsLetter(str[j]))
{
j++;
len++;
}
// Find frequency of preceding substring.
while (j < n && char.IsDigit(str[j]))
{
freq = freq * 10 + (str[j] - '0');
j++;
}
// Find length of substring when
// it is repeated.
num = freq * len;
// If length of repeated substring is less than
// k then required character is present in next
// substring. Subtract length of repeated
// substring from k to keep account of number of
// characters required to be visited.
if (k > num)
{
k -= num;
i = j;
}
// If length of repeated substring is
// more or equal to k then required
// character lies in current substring.
else
{
k--;
k %= len;
return str[i + k];
}
}
// This is for the case when there
// are no repetition in string.
// e.g. str="abced".
return str[k - 1];
}
// Driver Code
public static void Main(String[] args)
{
String str = "abced";
int k = 4;
Console.WriteLine(encodedChar(str.ToCharArray(), k));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
e
时间复杂度: O(n)
辅助空间: O(1)