📌  相关文章
📜  通过附加 S1 (M) 次和 S2 (M+1) 次查找第 K 个索引处的字符

📅  最后修改于: 2022-05-13 01:57:06.732000             🧑  作者: Mango

通过附加 S1 (M) 次和 S2 (M+1) 次查找第 K 个索引处的字符

给定两个字符串, S1S2以及一个整数K。可以通过执行以下操作来生成字符串str

  • 最初取一个空字符串
  • 追加 S1 一次
  • 附加 S2 两次
  • 追加 S1 3 次
  • 追加 S2 四次
  • 附加 S1 五次
  • 附加 S2 六次
  • 你可以继续构建这个字符串,直到它达到 K

任务是找到新字符str中第k位置的字符串。

方法:简单的解决方案是继续在字符串str中附加字符串,直到索引达到k。然后,将字符返回到索引k

下面是上述方法的实现。

C++
// C++ program to solve the above approach
#include
using namespace std;
 
char KthCharacter(string s, string t, long k)
{
 
  // initializing first and second variable
  // as to store how many string 's'
  // and string 't' will be appended
  long f = 1;
  long ss = 2;
  string tmp = "";
 
  // storing tmp length
  int len = tmp.length();
 
  // if length of string tmp is greater than k, then
  // we have reached our destination string now we can
  // return character at index k
  while (len < k) {
 
    long tf = f;
    long ts = ss;
 
    // appending s to tmp, f times
    while (tf-- != 0) {
      tmp += s;
    }
 
    // appending t to tmp, s times
    while (ts-- != 0) {
      tmp += t;
    }
    f += 2;
    ss += 2;
    len = tmp.length();
  }
 
  // extracting output character
  char output = tmp[k - 1];
  return output;
}
 
// Driver code
int main()
{
  string S1 = "a", S2 = "bc";
  int k = 4;
  char ans = KthCharacter(S1, S2, k);
  cout<


Java
// Java program to solve the above approach
 
import java.io.*;
 
class GFG {
    static char KthCharacter(String s, String t, long k)
    {
        // initializing first and second variable
        // as to store how many string 's'
        // and string 't' will be appended
        long f = 1;
        long ss = 2;
        String tmp = "";
 
        // storing tmp length
        int len = tmp.length();
 
        // if length of string tmp is greater than k, then
        // we have reached our destination string now we can
        // return character at index k
        while (len < k) {
 
            long tf = f;
            long ts = ss;
 
            // appending s to tmp, f times
            while (tf-- != 0) {
                tmp += s;
            }
 
            // appending t to tmp, s times
            while (ts-- != 0) {
                tmp += t;
            }
            f += 2;
            ss += 2;
            len = tmp.length();
        }
 
        // extracting output character
        char output = tmp.charAt((int)k - 1);
        return output;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        String S1 = "a", S2 = "bc";
        int k = 4;
        char ans = KthCharacter(S1, S2, k);
        System.out.println(ans);
    }
}


Python3
# Python program to solve the above approach
def KthCharacter(s, t, k):
   
    # initializing first and second variable
    # as to store how many string 's'
    # and string 't' will be appended
    f = 1
    ss = 2
    tmp = ""
     
    # storing tmp length
    lenn = len(tmp)
     
    # if length of string tmp is greater than k, then
    # we have reached our destination string now we can
    # return character at index k
    while (lenn < k):
        tf = f
        ts = ss
         
        # appending s to tmp, f times
        while (tf != 0):
            tf -=1
            tmp += s
        # appending t to tmp, s times
        while (ts != 0) :
            ts -=1
            tmp += t
         
        f += 2
        ss += 2
        lenn = len(tmp)
     
    # extracting output character
    output = tmp[k - 1]
    return output
     
# Driver code
S1 = "a"
S2 = "bc"
k = 4
ans = KthCharacter(S1, S2, k)
print(ans)
 
# This code is contributed by shivanisinghss2110


C#
// C# program to solve the above approach
using System;
 
class GFG {
    static char KthCharacter(string s, string t, long k)
    {
        // initializing first and second variable
        // as to store how many string 's'
        // and string 't' will be appended
        long f = 1;
        long ss = 2;
        string tmp = "";
 
        // storing tmp length
        int len = tmp.Length;
 
        // if length of string tmp is greater than k, then
        // we have reached our destination string now we can
        // return character at index k
        while (len < k) {
 
            long tf = f;
            long ts = ss;
 
            // appending s to tmp, f times
            while (tf-- != 0) {
                tmp += s;
            }
 
            // appending t to tmp, s times
            while (ts-- != 0) {
                tmp += t;
            }
            f += 2;
            ss += 2;
            len = tmp.Length;
        }
 
        // extracting output character
        char output = tmp[(int)k - 1];
        return output;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
 
        string S1 = "a", S2 = "bc";
        int k = 4;
        char ans = KthCharacter(S1, S2, k);
        Console.Write(ans);
    }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ program to solve the above approach
#include
using namespace std;
 
char KthCharacter(string s, string t, long k)
{
   
  // storing length
  long n = s.length();
  long m = t.length();
 
  // variables for temporary strings of s and t
  long first = 1;
  long second = 2;
 
  // final output variable
  char output = '?';
  while (k > 0) {
 
    // if k is greater than even after adding string
    // 's' 'first' times  (let's call it x) we'll
    // subtract x from k
    if (k > first * n) {
 
      long x = first * n;
      k = k - x;
 
      // incrementing first by 2, as said in
      // problem statement
      first = first + 2;
 
      // if k is greater than even after adding
      // string 't' 'second' times  (let's call it
      // y) we'll subtract y from k
      if (k > second * m) {
 
        long y = second * m;
        k = k - y;
 
        // incrementing second by two as said in
        // problem statement
        second = second + 2;
      }
 
      // if k is smaller than y, then the
      // character
      // will be at position k%m.
      else {
 
        // returning character at k index
        long ind = k % m;
        ind--;
        if (ind < 0)
          ind = m - 1;
        output = t[(int)ind];
        break;
      }
    }
 
    // if k is smaller than x, then the character
    // is at position k%n
    else {
 
      // returning character at k index
      long ind = k % n;
      ind--;
      if (ind < 0)
        ind = n - 1;
      output = s[(int)ind];
      break;
    }
  }
  return output;
}
 
// Driver code
int main()
{
 
  string S1 = "a", S2 = "bc";
  int k = 4;
  char ans = KthCharacter(S1, S2, k);
  cout<<(ans);
  return 0;
}
 
// This code is contributed by umadevi9616


Java
// Java program to solve the above approach
 
import java.io.*;
 
class GFG {
    static char KthCharacter(String s, String t, long k)
    {
        // storing length
        long n = s.length();
        long m = t.length();
 
        // variables for temporary strings of s and t
        long first = 1;
        long second = 2;
 
        // final output variable
        char output = '?';
        while (k > 0) {
 
            // if k is greater than even after adding string
            // 's' 'first' times  (let's call it x) we'll
            // subtract x from k
            if (k > first * n) {
 
                long x = first * n;
                k = k - x;
 
                // incrementing first by 2, as said in
                // problem statement
                first = first + 2;
 
                // if k is greater than even after adding
                // string 't' 'second' times  (let's call it
                // y) we'll subtract y from k
                if (k > second * m) {
 
                    long y = second * m;
                    k = k - y;
 
                    // incrementing second by two as said in
                    // problem statement
                    second = second + 2;
                }
 
                // if k is smaller than y, then the
                // character
                // will be at position k%m.
                else {
 
                    // returning character at k index
                    long ind = k % m;
                    ind--;
                    if (ind < 0)
                        ind = m - 1;
                    output = t.charAt((int)ind);
                    break;
                }
            }
 
            // if k is smaller than x, then the character
            // is at position k%n
            else {
 
                // returning character at k index
                long ind = k % n;
                ind--;
                if (ind < 0)
                    ind = n - 1;
                output = s.charAt((int)ind);
                break;
            }
        }
        return output;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        String S1 = "a", S2 = "bc";
        int k = 4;
        char ans = KthCharacter(S1, S2, k);
        System.out.println(ans);
    }
}


Python3
# Python program to solve the above approach
def KthCharacter(s, t, k):
     
        # storing length
        n = len(s)
        m = len(t)
 
        # variables for temporary strings of s and t
        first = 1
        second = 2
 
        # final output variable
        output = '?'
        while (k > 0):
 
            # if k is greater than even after adding string
            # 's' 'first' times  (let's call it x) we'll
            # subtract x from k
            if (k > first * n):
                x = first * n
                k = k - x
 
                # incrementing first by 2, as said in
                # problem statement
                first = first + 2
 
                # if k is greater than even after adding
                # string 't' 'second' times  (let's call it
                # y) we'll subtract y from k
                if (k > second * m):
 
                    y = second * m
                    k = k - y
 
                    # incrementing second by two as said in
                    # problem statement
                    second = second + 2
                 
                # if k is smaller than y, then the
                # character
                # will be at position k%m.
                else:
 
                    # returning character at k index
                    ind = k % m
                    ind-=1
                    if (ind < 0):
                        ind = m - 1
                    output = t[ind]
                    break
                 
            # if k is smaller than x, then the character
            # is at position k%n
            else:
 
                # returning character at k index
                ind = k % n
                ind-=1
                if (ind < 0):
                    ind = n - 1
                output = s[ind]
                break
             
        return output
     
# Driver code
S1 = "a"
S2 = "bc"
k = 4
ans = KthCharacter(S1, S2, k)
print(ans)
     
# This code is contributed by shivanisinghss2110


C#
// C# program to solve the above approach
using System;
 
public class GFG {
    static char Kthchar(String s, String t, long k)
    {
       
        // storing length
        long n = s.Length;
        long m = t.Length;
 
        // variables for temporary strings of s and t
        long first = 1;
        long second = 2;
 
        // readonly output variable
        char output = '?';
        while (k > 0) {
 
            // if k is greater than even after adding string
            // 's' 'first' times  (let's call it x) we'll
            // subtract x from k
            if (k > first * n) {
 
                long x = first * n;
                k = k - x;
 
                // incrementing first by 2, as said in
                // problem statement
                first = first + 2;
 
                // if k is greater than even after adding
                // string 't' 'second' times  (let's call it
                // y) we'll subtract y from k
                if (k > second * m) {
 
                    long y = second * m;
                    k = k - y;
 
                    // incrementing second by two as said in
                    // problem statement
                    second = second + 2;
                }
 
                // if k is smaller than y, then the
                // character
                // will be at position k%m.
                else {
 
                    // returning character at k index
                    long ind = k % m;
                    ind--;
                    if (ind < 0)
                        ind = m - 1;
                    output = t[(int)(ind)];
                    break;
                }
            }
 
            // if k is smaller than x, then the character
            // is at position k%n
            else {
 
                // returning character at k index
                long ind = k % n;
                ind--;
                if (ind < 0)
                    ind = n - 1;
                output = s[(int)(ind)];
                break;
            }
        }
        return output;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        String S1 = "a", S2 = "bc";
        int k = 4;
        char ans = Kthchar(S1, S2, k);
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by Amit Katiyar


Javascript


输出
b

时间复杂度: O(K)
辅助空间: O(K)

高效方法:由于Klong类型,并且字符串的大小只能达到 int 的最大可能值,因此不能使用此代码,也不会在每种情况下都通过。 不是将字符串st附加到temp ,而是从K中减去s的长度和t的长度,以便范围始终在int类型内。请按照以下步骤解决问题:

  • 将两个变量nm初始化为各自字符串st 的长度。
  • 初始化两个变量, firstsecond12以保持时间字符串st必须被追加的计数
  • 初始化变量output以将字符存储在新字符串中的第 k位置。
  • 在 while 循环中迭代直到k大于0:
    • 如果k大于first*n ,则从k中减去它并将first的值增加2。
    • 如果k大于second*m ,则从k中减去它并将second的值增加2。
    • 否则( k小于second*m),则第二个字符串t中的字符将作为答案,因为字符串t将被附加第二次,并且k将在此位置范围内。
    • 将变量ind初始化为k%m的模数,字符串tind位置的字符就是答案。将字符存储在变量输出中。
    • 否则( k小于first*n),则第二个字符串s中的字符将作为答案,因为字符串s第一次附加,并且k将在此位置范围内。
    • 将变量ind初始化为k%n的模数,字符串sind位置的字符就是答案。将字符存储在变量输出中。
  • 执行上述步骤后,返回输出值作为答案。

下面是上述方法的实现。

C++

// C++ program to solve the above approach
#include
using namespace std;
 
char KthCharacter(string s, string t, long k)
{
   
  // storing length
  long n = s.length();
  long m = t.length();
 
  // variables for temporary strings of s and t
  long first = 1;
  long second = 2;
 
  // final output variable
  char output = '?';
  while (k > 0) {
 
    // if k is greater than even after adding string
    // 's' 'first' times  (let's call it x) we'll
    // subtract x from k
    if (k > first * n) {
 
      long x = first * n;
      k = k - x;
 
      // incrementing first by 2, as said in
      // problem statement
      first = first + 2;
 
      // if k is greater than even after adding
      // string 't' 'second' times  (let's call it
      // y) we'll subtract y from k
      if (k > second * m) {
 
        long y = second * m;
        k = k - y;
 
        // incrementing second by two as said in
        // problem statement
        second = second + 2;
      }
 
      // if k is smaller than y, then the
      // character
      // will be at position k%m.
      else {
 
        // returning character at k index
        long ind = k % m;
        ind--;
        if (ind < 0)
          ind = m - 1;
        output = t[(int)ind];
        break;
      }
    }
 
    // if k is smaller than x, then the character
    // is at position k%n
    else {
 
      // returning character at k index
      long ind = k % n;
      ind--;
      if (ind < 0)
        ind = n - 1;
      output = s[(int)ind];
      break;
    }
  }
  return output;
}
 
// Driver code
int main()
{
 
  string S1 = "a", S2 = "bc";
  int k = 4;
  char ans = KthCharacter(S1, S2, k);
  cout<<(ans);
  return 0;
}
 
// This code is contributed by umadevi9616

Java

// Java program to solve the above approach
 
import java.io.*;
 
class GFG {
    static char KthCharacter(String s, String t, long k)
    {
        // storing length
        long n = s.length();
        long m = t.length();
 
        // variables for temporary strings of s and t
        long first = 1;
        long second = 2;
 
        // final output variable
        char output = '?';
        while (k > 0) {
 
            // if k is greater than even after adding string
            // 's' 'first' times  (let's call it x) we'll
            // subtract x from k
            if (k > first * n) {
 
                long x = first * n;
                k = k - x;
 
                // incrementing first by 2, as said in
                // problem statement
                first = first + 2;
 
                // if k is greater than even after adding
                // string 't' 'second' times  (let's call it
                // y) we'll subtract y from k
                if (k > second * m) {
 
                    long y = second * m;
                    k = k - y;
 
                    // incrementing second by two as said in
                    // problem statement
                    second = second + 2;
                }
 
                // if k is smaller than y, then the
                // character
                // will be at position k%m.
                else {
 
                    // returning character at k index
                    long ind = k % m;
                    ind--;
                    if (ind < 0)
                        ind = m - 1;
                    output = t.charAt((int)ind);
                    break;
                }
            }
 
            // if k is smaller than x, then the character
            // is at position k%n
            else {
 
                // returning character at k index
                long ind = k % n;
                ind--;
                if (ind < 0)
                    ind = n - 1;
                output = s.charAt((int)ind);
                break;
            }
        }
        return output;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        String S1 = "a", S2 = "bc";
        int k = 4;
        char ans = KthCharacter(S1, S2, k);
        System.out.println(ans);
    }
}

Python3

# Python program to solve the above approach
def KthCharacter(s, t, k):
     
        # storing length
        n = len(s)
        m = len(t)
 
        # variables for temporary strings of s and t
        first = 1
        second = 2
 
        # final output variable
        output = '?'
        while (k > 0):
 
            # if k is greater than even after adding string
            # 's' 'first' times  (let's call it x) we'll
            # subtract x from k
            if (k > first * n):
                x = first * n
                k = k - x
 
                # incrementing first by 2, as said in
                # problem statement
                first = first + 2
 
                # if k is greater than even after adding
                # string 't' 'second' times  (let's call it
                # y) we'll subtract y from k
                if (k > second * m):
 
                    y = second * m
                    k = k - y
 
                    # incrementing second by two as said in
                    # problem statement
                    second = second + 2
                 
                # if k is smaller than y, then the
                # character
                # will be at position k%m.
                else:
 
                    # returning character at k index
                    ind = k % m
                    ind-=1
                    if (ind < 0):
                        ind = m - 1
                    output = t[ind]
                    break
                 
            # if k is smaller than x, then the character
            # is at position k%n
            else:
 
                # returning character at k index
                ind = k % n
                ind-=1
                if (ind < 0):
                    ind = n - 1
                output = s[ind]
                break
             
        return output
     
# Driver code
S1 = "a"
S2 = "bc"
k = 4
ans = KthCharacter(S1, S2, k)
print(ans)
     
# This code is contributed by shivanisinghss2110

C#

// C# program to solve the above approach
using System;
 
public class GFG {
    static char Kthchar(String s, String t, long k)
    {
       
        // storing length
        long n = s.Length;
        long m = t.Length;
 
        // variables for temporary strings of s and t
        long first = 1;
        long second = 2;
 
        // readonly output variable
        char output = '?';
        while (k > 0) {
 
            // if k is greater than even after adding string
            // 's' 'first' times  (let's call it x) we'll
            // subtract x from k
            if (k > first * n) {
 
                long x = first * n;
                k = k - x;
 
                // incrementing first by 2, as said in
                // problem statement
                first = first + 2;
 
                // if k is greater than even after adding
                // string 't' 'second' times  (let's call it
                // y) we'll subtract y from k
                if (k > second * m) {
 
                    long y = second * m;
                    k = k - y;
 
                    // incrementing second by two as said in
                    // problem statement
                    second = second + 2;
                }
 
                // if k is smaller than y, then the
                // character
                // will be at position k%m.
                else {
 
                    // returning character at k index
                    long ind = k % m;
                    ind--;
                    if (ind < 0)
                        ind = m - 1;
                    output = t[(int)(ind)];
                    break;
                }
            }
 
            // if k is smaller than x, then the character
            // is at position k%n
            else {
 
                // returning character at k index
                long ind = k % n;
                ind--;
                if (ind < 0)
                    ind = n - 1;
                output = s[(int)(ind)];
                break;
            }
        }
        return output;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        String S1 = "a", S2 = "bc";
        int k = 4;
        char ans = Kthchar(S1, S2, k);
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by Amit Katiyar

Javascript


输出
b

时间复杂度: O(K)
辅助空间: O(1)