📌  相关文章
📜  解密字符串后找到第k个字符的程序

📅  最后修改于: 2021-05-31 20:35:20             🧑  作者: Mango

给定一个字符串str由字符和数字和整数k的,任务是解密字符串和在经解密的字符串返回k字符。

为了解密字符串,通过字符遍历字符串字符,如果当前字符是字母,然后将其追加到别的结果字符串,如果它是那么数字位解析的数量和重复得到的字符串的时候,这解析的数量和继续原始字符串。例如,str =“ ab2c3”将被解密为“ ababcababcababc”。

例子:

方法:

  • 初始化起始索引i = 0total_len = 0
  • i小于输入字符串的长度时循环播放,并检查当前字符是否为字母。如果是,则将total_len递增1,并检查总长度是否小于或等于k(如果是),然后返回字符串,否则递增i
  • 初始化n = 0再次循环,而i小于输入字符串的长度且i不是字母,然后解析数字并递增i并找到next_total_len = total_len * n。
    • 如果k 则通过初始化pos = k%total_len来获得字符的位置。
    • 如果找不到位置,则更新position = total_len ,最后返回第k字符。如果找不到,则返回-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
using namespace std;
  
// Function to print kth character of
// String s after decrypting it
char findKthChar(string s, int k)
{
  
    // Get the length of string
    int len = s.length();
  
    // Initialise pointer to character
    // of input string to zero
    int i = 0;
  
    // Total length of resultant string
    int total_len = 0;
  
    // Traverse the string from starting
    // and check if each character is
    // alphabet then increment total_len
    while (i < len) {
        if (isalpha(s[i])) {
  
            total_len++;
  
            // If total_leg equal to k then
            // return string else increment i
            if (total_len == k)
                return s[i];
  
            i++;
        }
  
        else {
  
            // Parse the number
            int n = 0;
            while (i < len && !isalpha(s[i])) {
                n = n * 10 + (s[i] - '0');
                i++;
            }
  
            // Update next_total_len
            int next_total_len = total_len * n;
  
            // Get the position of kth character
            if (k <= next_total_len) {
                int pos = k % total_len;
  
                // Position not found then update
                // position with total_len
                if (!pos) {
                    pos = total_len;
                }
  
                // Recursively find the kth position
                return findKthChar(s, pos);
            }
            else {
  
                // Else update total_len
                // by next_total_len
                total_len = next_total_len;
            }
        }
    }
  
    // Return -1 if character not found
    return -1;
}
  
// Driver code
int main()
{
    string s = "ab2c3";
    int k = 5;
  
    cout << findKthChar(s, k);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
class GfG 
{ 
  
// Function to print kth character of 
// String s after decrypting it 
static Character findKthChar(String s, int k) 
{ 
  
    // Get the length of string 
    int len = s.length(); 
  
    // Initialise pointer to character 
    // of input string to zero 
    int i = 0; 
  
    // Total length of resultant string 
    int total_len = 0; 
  
    // Traverse the string from starting 
    // and check if each character is 
    // alphabet then increament total_len 
    while (i < len)
    { 
        if (Character.isLetter(s.charAt(i)))
        { 
  
            total_len++; 
  
            // If total_leg equal to k then 
            // return string else increment i 
            if (total_len == k) 
                return s.charAt(i); 
  
            i++; 
        } 
  
        else
        { 
  
            // Parse the number 
            int n = 0; 
            while (i < len && !Character.isLetter(s.charAt(i)))
            { 
                n = n * 10 + (s.charAt(i) - '0'); 
                i++; 
            } 
  
            // Update next_total_len 
            int next_total_len = total_len * n; 
  
            // Get the position of kth character 
            if (k <= next_total_len)
            { 
                int pos = k % total_len; 
  
                // Position not found then update 
                // position with total_len 
                if (pos == 0)
                { 
                    pos = total_len; 
                } 
  
                // Recursively find the kth position 
                return findKthChar(s, pos); 
            } 
            else 
            { 
  
                // Else update total_len 
                // by next_total_len 
                total_len = next_total_len; 
            } 
        } 
    } 
  
    // Return -1 if character not found 
    return ' '; 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    String s = "ab2c3"; 
    int k = 5; 
  
    System.out.println(findKthChar(s, k)); 
}
} 
  
// This code is contributed by Prerna Saini.


Python3
# Python 3 implementation of the approach
  
# Function to print kth character of
# String s after decrypting it
def findKthChar(s, k):
      
    # Get the length of string
    len1 = len(s)
  
    # Initialise pointer to character
    # of input string to zero
    i = 0
  
    # Total length of resultant string
    total_len = 0
  
    # Traverse the string from starting
    # and check if each character is
    # alphabet then increament total_len
    while (i < len1):
        if (s[i].isalpha()):
            total_len += 1
  
            # If total_leg equal to k then
            # return string else increment i
            if (total_len == k):
                return s[i]
  
            i += 1
  
        else:
              
            # Parse the number
            n = 0
            while (i < len1 and s[i].isalpha() == False):
                n = n * 10 + (ord(s[i]) - ord('0'))
                i += 1
  
            # Update next_total_len
            next_total_len = total_len * n
  
            # Get the position of kth character
            if (k <= next_total_len):
                pos = k % total_len
  
                # Position not found then update
                # position with total_len
                if (pos == 0):
                    pos = total_len
  
                # Recursively find the kth position
                return findKthChar(s, pos)
  
            else:
                  
                # Else update total_len
                # by next_total_len
                total_len = next_total_len
  
    # Return -1 if character not found
    return -1
  
# Driver code
if __name__ == '__main__':
    s = "ab2c3"
    k = 5
  
    print(findKthChar(s, k))
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System; 
  
class GFG 
{ 
  
// Function to print kth character of 
// String s after decrypting it 
static char findKthChar(String s, int k) 
{ 
  
    // Get the length of string 
    int len = s.Length; 
  
    // Initialise pointer to character 
    // of input string to zero 
    int i = 0; 
  
    // Total length of resultant string 
    int total_len = 0; 
  
    // Traverse the string from starting 
    // and check if each character is 
    // alphabet then increament total_len 
    while (i < len)
    { 
        if (char.IsLetter(s[i]))
        { 
            total_len++; 
  
            // If total_leg equal to k then 
            // return string else increment i 
            if (total_len == k) 
                return s[i]; 
  
            i++; 
        } 
  
        else
        { 
  
            // Parse the number 
            int n = 0; 
            while (i < len && !char.IsLetter(s[i]))
            { 
                n = n * 10 + (s[i] - '0'); 
                i++; 
            } 
  
            // Update next_total_len 
            int next_total_len = total_len * n; 
  
            // Get the position of kth character 
            if (k <= next_total_len)
            { 
                int pos = k % total_len; 
  
                // Position not found then update 
                // position with total_len 
                if (pos == 0)
                { 
                    pos = total_len; 
                } 
  
                // Recursively find the kth position 
                return findKthChar(s, pos); 
            } 
            else
            { 
  
                // Else update total_len 
                // by next_total_len 
                total_len = next_total_len; 
            } 
        } 
    } 
  
    // Return -1 if character not found 
    return ' '; 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    String s = "ab2c3"; 
    int k = 5; 
  
    Console.WriteLine(findKthChar(s, k)); 
}
}
  
// This code is contributed by PrinciRaj1992


输出:
c

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。