Given two strings, S1 and S2 and an integer K.A string str can be made by performing the following operations:
- Take an Empty String Initially
- Append S1 one times
- Append S2 two times
- Append S1 three times
- Append S2 four times
- Append S1 five times
- Append S2 six times
- You can keep building this string until it reaches K
The task is to find the character at the kth position in the new string str.
Input: S1 = “a”, S2 = “bc”, k = 4
Output: b
Explanation: s3 = “a” + “bcbc” +”aaa” + “bcbcbcbc” ==> abcbcaaabcbcbcbc. so the character on the 4th index is ‘b’
Input: S1 = “Geeks”, S2 = “for”, k = 3
Output: e
Explanation: s3 = “Geeks” + “for” + “GeeksGeeks” ==> GeeksforGeeksGeeks. so the character at 3rd position is ‘e’.
Approach: The simple solution would be to keep appending strings in the string str, until the index reaches k. Then, return the character to the index k.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
char KthCharacter(string s, string t, long k)
{
long f = 1;
long ss = 2;
string tmp = "" ;
int len = tmp.length();
while (len < k) {
long tf = f;
long ts = ss;
while (tf-- != 0) {
tmp += s;
}
while (ts-- != 0) {
tmp += t;
}
f += 2;
ss += 2;
len = tmp.length();
}
char output = tmp[k - 1];
return output;
}
int main()
{
string S1 = "a" , S2 = "bc" ;
int k = 4;
char ans = KthCharacter(S1, S2, k);
cout<<ans;
}
|
Java
import java.io.*;
class GFG {
static char KthCharacter(String s, String t, long k)
{
long f = 1 ;
long ss = 2 ;
String tmp = "" ;
int len = tmp.length();
while (len < k) {
long tf = f;
long ts = ss;
while (tf-- != 0 ) {
tmp += s;
}
while (ts-- != 0 ) {
tmp += t;
}
f += 2 ;
ss += 2 ;
len = tmp.length();
}
char output = tmp.charAt(( int )k - 1 );
return output;
}
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
def KthCharacter(s, t, k):
f = 1
ss = 2
tmp = ""
lenn = len (tmp)
while (lenn < k):
tf = f
ts = ss
while (tf ! = 0 ):
tf - = 1
tmp + = s
while (ts ! = 0 ) :
ts - = 1
tmp + = t
f + = 2
ss + = 2
lenn = len (tmp)
output = tmp[k - 1 ]
return output
S1 = "a"
S2 = "bc"
k = 4
ans = KthCharacter(S1, S2, k)
print (ans)
|
C#
using System;
class GFG {
static char KthCharacter( string s, string t, long k)
{
long f = 1;
long ss = 2;
string tmp = "" ;
int len = tmp.Length;
while (len < k) {
long tf = f;
long ts = ss;
while (tf-- != 0) {
tmp += s;
}
while (ts-- != 0) {
tmp += t;
}
f += 2;
ss += 2;
len = tmp.Length;
}
char output = tmp[( int )k - 1];
return output;
}
public static void Main( string [] args)
{
string S1 = "a" , S2 = "bc" ;
int k = 4;
char ans = KthCharacter(S1, S2, k);
Console.Write(ans);
}
}
|
Javascript
<script>
function KthCharacter(s, t, k) {
let f = 1;
let ss = 2;
let tmp = "" ;
let len = tmp.length;
while (len < k) {
let tf = f;
let ts = ss;
while (tf-- != 0) {
tmp += s;
}
while (ts-- != 0) {
tmp += t;
}
f += 2;
ss += 2;
len = tmp.length;
}
let output = tmp[k - 1];
return output;
}
let S1 = "a" ,
S2 = "bc" ;
let k = 4;
let ans = KthCharacter(S1, S2, k);
document.write(ans);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(K)
Efficient Approach: Since K is a long type, and the size of a string can only get up to the max possible value of int, this code cannot be used and won’t pass in every case. Rather than appending the string s and t to temp, subtract the length of s and length of t from K, so that the range is always within int type. Follow the steps below to solve the problem:
- Initialize two variables n and m as the lengths of the respective strings s and t.
- Initialize two variables, first and second as 1 and 2 to keep the count of time strings s and t have to be appended.
- Initialize the variable output to store the character at kth position in the new string.
- Iterate in a while loop till k is greater than 0:
- If k is greater than first*n, then, subtract that from k and increase the value of first by 2.
- If k is greater than second*m, then, subtract that from k and increase the value of second by 2.
- Else(k is less than second*m), then the character from the second string t will be the answer as the string t will be appended second times and k will be in this range of positions.
- Initialize the variable ind as the modulus of k%m and the character at ind position in the string t will be the answer. Store the character in the variable output.
- Else(k is less than first*n), then the character from the second string s will be the answer as the string s will be appended first times and k will be in this range of positions.
- Initialize the variable ind as the modulus of k%n and the character at ind position in the string s will be the answer. Store the character in the variable output.
- After performing the above steps, return the value of output as the answer.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
char KthCharacter(string s, string t, long k)
{
long n = s.length();
long m = t.length();
long first = 1;
long second = 2;
char output = '?' ;
while (k > 0) {
if (k > first * n) {
long x = first * n;
k = k - x;
first = first + 2;
if (k > second * m) {
long y = second * m;
k = k - y;
second = second + 2;
}
else {
long ind = k % m;
ind--;
if (ind < 0)
ind = m - 1;
output = t[( int )ind];
break ;
}
}
else {
long ind = k % n;
ind--;
if (ind < 0)
ind = n - 1;
output = s[( int )ind];
break ;
}
}
return output;
}
int main()
{
string S1 = "a" , S2 = "bc" ;
int k = 4;
char ans = KthCharacter(S1, S2, k);
cout<<(ans);
return 0;
}
|
Java
import java.io.*;
class GFG {
static char KthCharacter(String s, String t, long k)
{
long n = s.length();
long m = t.length();
long first = 1 ;
long second = 2 ;
char output = '?' ;
while (k > 0 ) {
if (k > first * n) {
long x = first * n;
k = k - x;
first = first + 2 ;
if (k > second * m) {
long y = second * m;
k = k - y;
second = second + 2 ;
}
else {
long ind = k % m;
ind--;
if (ind < 0 )
ind = m - 1 ;
output = t.charAt(( int )ind);
break ;
}
}
else {
long ind = k % n;
ind--;
if (ind < 0 )
ind = n - 1 ;
output = s.charAt(( int )ind);
break ;
}
}
return output;
}
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
def KthCharacter(s, t, k):
n = len (s)
m = len (t)
first = 1
second = 2
output = '?'
while (k > 0 ):
if (k > first * n):
x = first * n
k = k - x
first = first + 2
if (k > second * m):
y = second * m
k = k - y
second = second + 2
else :
ind = k % m
ind - = 1
if (ind < 0 ):
ind = m - 1
output = t[ind]
break
else :
ind = k % n
ind - = 1
if (ind < 0 ):
ind = n - 1
output = s[ind]
break
return output
S1 = "a"
S2 = "bc"
k = 4
ans = KthCharacter(S1, S2, k)
print (ans)
|
C#
using System;
public class GFG {
static char Kthchar(String s, String t, long k)
{
long n = s.Length;
long m = t.Length;
long first = 1;
long second = 2;
char output = '?' ;
while (k > 0) {
if (k > first * n) {
long x = first * n;
k = k - x;
first = first + 2;
if (k > second * m) {
long y = second * m;
k = k - y;
second = second + 2;
}
else {
long ind = k % m;
ind--;
if (ind < 0)
ind = m - 1;
output = t[( int )(ind)];
break ;
}
}
else {
long ind = k % n;
ind--;
if (ind < 0)
ind = n - 1;
output = s[( int )(ind)];
break ;
}
}
return output;
}
public static void Main(String[] args)
{
String S1 = "a" , S2 = "bc" ;
int k = 4;
char ans = Kthchar(S1, S2, k);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function KthCharacter(s, t , k)
{
var n = s.length;
var m = t.length;
var first = 1;
var second = 2;
var output = '?' ;
while (k > 0) {
if (k > first * n) {
var x = first * n;
k = k - x;
first = first + 2;
if (k > second * m) {
var y = second * m;
k = k - y;
second = second + 2;
}
else {
var ind = k % m;
ind--;
if (ind < 0)
ind = m - 1;
output = t.charAt(parseInt(ind));
break ;
}
}
else {
var ind = k % n;
ind--;
if (ind < 0)
ind = n - 1;
output = s.charAt(parseInt(ind));
break ;
}
}
return output;
}
var S1 = "a" , S2 = "bc" ;
var k = 4;
var ans = KthCharacter(S1, S2, k);
document.write(ans);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!