求最深字符串Output the most inner string
-
Output the most inner string: "ab(cd(e)fg)" => "e", "Hello World!" =>"Hello World!" "ab{cd[ef]} gh(hello{hi})" =>"ef", "hi"
先用https://www.geeksforgeeks.org/find-maximum-depth-nested-parenthesis-string/
// Traverse the input string for (int i = 0; i < n; i++) { if (S.charAt(i) == '(') { current_max++; // update max if required if (current_max > max) { max = current_max; } } else if (S.charAt(i) == ')') { if (current_max > 0) { current_max--; } else { return -1; } } }
max和current_max求出最大深度。然后再来一次,最大深度的时候append
或者用:https://stackoverflow.com/questions/12595019/how-to-get-a-string-between-two-characters
一个不需要您执行indexOf的问题的非常有用的解决方案是使用Apache Commons库。
StringUtils.substringBetween(s, "(", ")");