-
Notifications
You must be signed in to change notification settings - Fork 0
/
BEADS.java
42 lines (42 loc) · 977 Bytes
/
BEADS.java
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
package ques;
import java.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BEADS {
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t>0){
String s=br.readLine();
int l =s.length();
char sw=s.charAt(0);
for(int i=1;i<l;i++){
if(s.charAt(i)<sw)
sw=s.charAt(i);
}
ArrayList<Integer> arr=new ArrayList<Integer>();
for(int i=0;i<l;i++){
if(s.charAt(i)==sw)
arr.add(i);
}
s=s+s;
int min=arr.get(0);
for(int i=1;i<arr.size();i++){
for(int j=1;j<s.length()/2;j++){
if(s.charAt((min+j))>s.charAt((arr.get(i)+j))){
min=arr.get(i);
break;
}
else if(s.charAt((min+j))<s.charAt((arr.get(i)+j))){
break;
}
}
}
System.out.println(min+1);
t--;
}
}
}