-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleStringIndices.java
More file actions
36 lines (31 loc) · 881 Bytes
/
SimpleStringIndices.java
File metadata and controls
36 lines (31 loc) · 881 Bytes
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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
// https://www.codewars.com/kata/5a24254fe1ce0ec2eb000078
public class SimpleStringIndices {
private final String input;
private final int indexOfStart;
public SimpleStringIndices(
String input,
int indexOfStart
) {
this.input = input;
this.indexOfStart = indexOfStart;
}
public int solution() {
int openBrackets = 0;
if (!(input.charAt(indexOfStart) == '(')) {
return -1;
}
for (int i = indexOfStart; i < input.length(); i++) {
if (input.charAt(i) == '(') {
openBrackets++;
}
if (input.charAt(i) == ')') {
openBrackets--;
}
if (openBrackets == 0) {
return i;
}
}
return -1;
}
}