-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGSOA.java
More file actions
45 lines (38 loc) · 1.46 KB
/
GSOA.java
File metadata and controls
45 lines (38 loc) · 1.46 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
import java.util.ArrayList;
public class GSOA {
private static ArrayList<String> PossibleSubStringswithMAjorInvestments(String string) {
ArrayList<String> result = new ArrayList<String>();
int countA = 0, countB = 0, countC = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == 'A')
countA++;
if (string.charAt(i) == 'B')
countB++;
if (string.charAt(i) == 'C')
countC++;
}
for (int i = 0; i < string.length(); i++) {
int j = i;
boolean investmentA = false;
boolean investmentB = false;
boolean investmentC = false;
while (j < string.length()) {
if (string.charAt(j) == 'A')
investmentA = true;
if (string.charAt(j) == 'B')
investmentB = true;
if (string.charAt(j) == 'C')
investmentC = true;
if ((investmentA || countA == 0) && (investmentB || countB == 0) && (investmentC || countC == 0)) {
result.add(string.substring(i, j + 1));
}
j++;
}
}
return result;
}
public static void main(String[] args) {
//System.out.println(PossibleSubStringswithMAjorInvestments("ABBCZBAC"));
System.out.println(PossibleSubStringswithMAjorInvestments("PQACBA"));
}
}