给定一个编码字符串,其中子字符串的重复表示为子字符串,后跟子字符串的计数。例如,如果加密字符串为“ab2cd2”且k=4,则输出将为“b”,因为解密字符串为“ababcdcd”,第四个字符为“b”。 注: 加密子字符串的频率可以超过一位数。例如,在“ab12c3”中,ab重复12次。子串的频率中不存在前导0。 例如:
null
Input: "a2b2c3", k = 5Output: cDecrypted string is "aabbccc"Input : "ab4c2ed3", k = 9Output : cDecrypted string is "ababababccededed"Input: "ab4c12ed3", k = 21Output: eDecrypted string is "ababababccccccccccccededed"
这个想法很简单。首先获取空的解密字符串,然后通过逐个读取子字符串及其频率来解压该字符串,并根据其频率将当前子字符串附加到解密字符串中。重复此过程直到字符串结束,并打印解密字符串中的第K个字符。
C++
// C++ program to find K'th character in // decrypted string #include<bits/stdc++.h> using namespace std; // Function to find K'th character in Encoded String char encodedChar(string str, int k) { // expand string variable is used to // store final string after decompressing string str string expand = "" ; string temp; // Current substring int freq = 0; // Count of current substring for ( int i=0; str[i]!= ' ' ; ) { temp = "" ; // Current substring freq = 0; // count frequency of current substring // read characters until you find a number // or end of string while (str[i]>= 'a' && str[i]<= 'z' ) { // push character in temp temp.push_back(str[i]); i++; } // read number for how many times string temp // will be repeated in decompressed string while (str[i]>= '1' && str[i]<= '9' ) { // generating frequency of temp freq = freq*10 + str[i] - '0' ; i++; } // now append string temp into expand // equal to its frequency for ( int j=1; j<=freq; j++) expand.append(temp); } // this condition is to handle the case // when string str is ended with alphabets // not with numeric value if (freq==0) expand.append(temp); return expand[k-1]; } // Driver program to test the string int main() { string str = "ab4c12ed3" ; int k = 21; cout << encodedChar(str, k) << endl; return 0; } |
JAVA
// Java program to find K'th character in // decrypted string public class GFG { // Function to find K'th character in // Encoded String static char encodedChar(String str, int k) { // expand string variable is used to // store final string after decompressing // string str String expand = "" ; String temp = "" ; // Current substring int freq = 0 ; // Count of current substring for ( int i= 0 ; i < str.length() ; ) { temp = "" ; // Current substring freq = 0 ; // count frequency of current // substring // read characters until you find a number // or end of string while (i < str.length() && str.charAt(i)>= 'a' && str.charAt(i)<= 'z' ) { // push character in temp temp += str.charAt(i); i++; } // read number for how many times string temp // will be repeated in decompressed string while (i < str.length() && str.charAt(i)>= '1' && str.charAt(i)<= '9' ) { // generating frequency of temp freq = freq* 10 + str.charAt(i) - '0' ; i++; } // now append string temp into expand // equal to its frequency for ( int j= 1 ; j<=freq; j++) expand += temp; } // this condition is to handle the case // when string str is ended with alphabets // not with numeric value if (freq== 0 ) expand += temp; return expand.charAt(k- 1 ); } // Driver program to test the string public static void main(String args[]) { String str = "ab4c12ed3" ; int k = 21 ; System.out.println(encodedChar(str, k)); } } // This code is contributed by Sumit Ghosh |
Python3
# Python 3 program to find K'th character # in decrypted string # Function to find K'th character # in Encoded String def encodedChar( str , k): # expand string variable is used # to store final string after # decompressing string str expand = "" # Current substring freq = 0 # Count of current substring i = 0 while (i < len ( str )): temp = "" # Current substring freq = 0 # count frequency of current substring # read characters until you find # a number or end of string while (i < len ( str ) and ord ( str [i]) > = ord ( 'a' ) and ord ( str [i]) < = ord ( 'z' )): # push character in temp temp + = str [i] i + = 1 # read number for how many times string temp # will be repeated in decompressed string while (i < len ( str ) and ord ( str [i]) > = ord ( '1' ) and ord ( str [i]) < = ord ( '9' )): # generating frequency of temp freq = freq * 10 + ord ( str [i]) - ord ( '0' ) i + = 1 # now append string temp into expand # equal to its frequency for j in range ( 1 , freq + 1 , 1 ): expand + = temp # this condition is to handle the case # when string str is ended with alphabets # not with numeric value if (freq = = 0 ): expand + = temp return expand[k - 1 ] # Driver Code if __name__ = = '__main__' : str = "ab4c12ed3" k = 21 print (encodedChar( str , k)) # This code is contributed by # Shashank_Sharma |
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( string str, int k) { // expand string variable is // used to store final string // after decompressing string str String expand = "" ; String temp = "" ; // Current substring int freq = 0; // Count of current substring for ( int i = 0; i < str.Length ; ) { temp = "" ; // Current substring freq = 0; // count frequency of current // substring // read characters until you // find a number or end of string while (i < str.Length && str[i]>= 'a' && str[i]<= 'z' ) { // push character in temp temp += str[i]; i++; } // read number for how many times // string temp will be repeated // in decompressed string while (i < str.Length && str[i] >= '1' && str[i] <= '9' ) { // generating frequency of temp freq = freq * 10 + str[i] - '0' ; i++; } // now append string temp into // expand equal to its frequency for ( int j = 1; j <= freq; j++) expand += temp; } // this condition is to handle // the case when string str is // ended with alphabets not // with numeric value if (freq == 0) expand += temp; return expand[k - 1]; } // Driver Code public static void Main() { string str = "ab4c12ed3" ; int k = 21; Console.Write(encodedChar(str, k)); } } // This code is contributed // by ChitraNayal |
Javascript
<script> // Javascript program to find K'th character in // decrypted string // Function to find K'th character in // Encoded String function encodedChar(str, k) { // expand string variable is used to // store final string after decompressing // string str let expand = "" ; let temp = "" ; // Current substring let freq = 0; // Count of current substring for (let i=0; i < str.length ; ) { temp = "" ; // Current substring freq = 0; // count frequency of current // substring // read characters until you find a number // or end of string while (i < str.length && str[i].charCodeAt(0)>= 'a' .charCodeAt(0) && str[i].charCodeAt(0)<= 'z' .charCodeAt(0)) { // push character in temp temp += str[i]; i++; } // read number for how many times string temp // will be repeated in decompressed string while (i < str.length && str[i].charCodeAt(0)>= '1' .charCodeAt(0) && str[i].charCodeAt(0)<= '9' .charCodeAt(0)) { // generating frequency of temp freq = freq*10 + str[i].charCodeAt(0) - '0' .charCodeAt(0); i++; } // now append string temp into expand // equal to its frequency for (let j=1; j<=freq; j++) expand += temp; } // this condition is to handle the case // when string str is ended with alphabets // not with numeric value if (freq==0) expand += temp; return expand[k-1]; } // Driver program to test the string let str = "ab4c12ed3" ; let k = 21; document.write(encodedChar(str, k)); // This code is contributed by avanitrachhadiya2155 </script> |
输出:
e
练习: 上述解决方案构建解码字符串以查找第k个字符。将解决方案扩展为在O(1)额外空间中工作。 查找解密字符串的第k个字符|集–2 本文由 沙申克·米什拉(古卢) 并由Geeksforgeks团队审核。如果你喜欢Geeksforgek,并且想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END