-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryPatternFinder.java
More file actions
50 lines (38 loc) · 1.6 KB
/
BinaryPatternFinder.java
File metadata and controls
50 lines (38 loc) · 1.6 KB
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
import java.util.Scanner;
public class BinaryPatternFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Binary Pattern Finder - 0(1+)0");
System.out.println("------------------------------");
System.out.print("Enter a binary string (containing only 0s and 1s): ");
String binaryString = scanner.nextLine();
int count = countPatterns(binaryString);
System.out.println("Input: " + binaryString);
System.out.println("Output: " + count);
scanner.close();
}
private static int countPatterns(String str) {
int count = 0;
// We need at least 3 characters for the pattern 0(1+)0
if (str.length() < 3) {
return 0;
}
for (int i = 0; i < str.length() - 2; i++) {
// Check if current character is '0'
if (str.charAt(i) == '0') {
// Find the next '0' after at least one '1'
int j = i + 1;
// Skip all consecutive 1s
while (j < str.length() && str.charAt(j) == '1') {
j++;
}
// If we found at least one '1' followed by a '0', we have a pattern
if (j < str.length() && j > i + 1 && str.charAt(j) == '0') {
count++;
// Note: We don't increment i here to allow overlapping patterns
}
}
}
return count;
}
}