-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesisBit.java
More file actions
22 lines (18 loc) · 829 Bytes
/
Copy pathParenthesisBit.java
File metadata and controls
22 lines (18 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//https://codingbat.com/prob/p137918
/*
Given a string that contains a single pair of parenthesis, compute recursively a new string made of only of the parenthesis and their contents, so "xyz(abc)123" yields "(abc)".
parenBit("xyz(abc)123") → "(abc)"
parenBit("x(hello)") → "(hello)"
parenBit("(xy)1") → "(xy)"
*/
public String parenBit(String str) {
if(str == null) return str;
else if(str.length() > 0 && str.substring(0, 1).equals(")")) return ")";
//else if(str.contains(")")) return ")";
//str.charAt(0)==')'
if(str.substring(0, 1).equals("(") || !str.contains("(") && str.contains(")"))
return str.substring(0, 1)+parenBit(str.substring(1));
//else if(!str.contains("(") && str.contains(")"))
//return str.substring(0, 1)+parenBit(str.substring(1));
return parenBit(str.substring(1));
}