-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind the n-th character
54 lines (42 loc) · 1.17 KB
/
Find the n-th character
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gfg;
//Find the n-th character
import java.io.*;
import java.util.*;
class GfG
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
String S = sc.next();
int R = sc.nextInt();
int N = sc.nextInt();
Solution obj = new Solution();
System.out.println(obj.nthCharacter(S,R,N));
}
}
}
class Solution
{
public char nthCharacter(String s, int r, int n)
{
for(int i = 0; i < r; i++) {
StringBuilder str = new StringBuilder();
for(char c: s.toCharArray()) {
if(c == '1') {
str.append("10");
}
else if(c == '0') {
str.append("01");
}
if(str.length() > s.length()) {
break;
}
}
s = str.toString();
}
return s.charAt(n);
}
}