Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- String s = sc.next();
- int res = smallestSubstring(n, s);
- System.out.println(res);
- }
- public static int smallestSubstring(int n, String s) {
- char[] temp = s.toCharArray();
- // Check for length of 2
- for (int i = 0; i < n - 1; i++) {
- if (temp[i] == 'x' && temp[i + 1] == 'x')
- return 2;
- }
- // Check for length of 3
- for (int i = 0; i < n - 2; i++) {
- if (temp[i] == 'x' && temp[i + 2] == 'x')
- return 3;
- }
- // Check for length of 4
- for (int i = 0; i < n - 3; i++) {
- if (temp[i] == 'x' && temp[i + 3] == 'x' && temp[i + 1] != temp[i + 2])
- return 4;
- }
- // Check for length of 7
- for (int i = 0; i < n - 6; i++) {
- if (temp[i] == 'x' && temp[i + 3] == 'x' && temp[i + 6] == 'x') {
- int countY = 0, countZ = 0;
- for (int j = i + 1; j <= i + 5; j++) {
- if (temp[j] == 'y')
- countY++;
- else if (temp[j] == 'z')
- countZ++;
- }
- if (countY == 2 && countZ == 2)
- return 7;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement