Skip to content

Commit bd6c829

Browse files
committed
commit de la solution de parenthes
1 parent 55c298f commit bd6c829

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/parentheseExo/Principale.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)