-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_01_2024.java
45 lines (38 loc) · 1.41 KB
/
03_01_2024.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
43
44
45
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String S = read.readLine();
Solution ob = new Solution();
int ans = ob.smallestSubstring(S);
System.out.println(ans);
}
}
}
// } Driver Code Ends
class Solution {
public int smallestSubstring(String S) {
int[] count = new int[3]; // to store count of 0, 1, and 2
int left = 0; // left pointer of the window
int minLength = Integer.MAX_VALUE; // to store the minimum length of the substring
for (int right = 0; right < S.length(); right++) {
char currentChar = S.charAt(right);
count[currentChar - '0']++;
// Move the left pointer to minimize the window
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
minLength = Math.min(minLength, right - left + 1);
char leftChar = S.charAt(left);
count[leftChar - '0']--;
left++;
}
}
return (minLength == Integer.MAX_VALUE) ? -1 : minLength;
}
}