File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package parentheseExo ;
2+
3+ import java .util .Stack ;
4+
5+ public class Principale {
6+ public static void main (String ... args ) {
7+
8+ String phrase = "je suis (rafael [nadal]de mallorca)" ;
9+ System .out .println (Principale .estParenthese (phrase ));
10+ }
11+
12+ public static boolean estParenthese (String phrase ) {
13+ Stack <String > stack = new Stack <>();
14+
15+ for (int i = 0 ; i < phrase .length (); i ++) {
16+ if (phrase .charAt (i ) == '(' ) {
17+ stack .push (")" );
18+ } else if (phrase .charAt (i ) == '[' ) {
19+ stack .push ("]" );
20+ }
21+
22+ else if (phrase .charAt (i ) == ')' && stack .peek ().equals (")" )) {
23+ stack .pop ();
24+ } else if (phrase .charAt (i ) == ']' && stack .peek ().equals ("]" )) {
25+ stack .pop ();
26+ }
27+
28+ }
29+ if (!stack .empty () || stack .size () != 0 ) {
30+ return false ;
31+ }
32+ return true ;
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments