-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab7.java
43 lines (41 loc) · 1.09 KB
/
Lab7.java
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
37
38
39
40
41
42
43
import java.util.*;
public class Lab7 {
public boolean recognize(String str) {
Stack<Character> s = new Stack<Character>();
int index = 0;
s.push('c');
char next = str.charAt(index);
while (index < str.length()) {
if (next == ' ') {
return false;
} else {
s.push(next);
index++;
next = str.charAt(index);
}
}
index++;
while (!s.isEmpty() && index < str.length()) {
next = str.charAt(index);
if (next != s.pop()) {
return false;
}
index++;
}
if (next == ' ') {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
Lab7 lab = new Lab7();
if (lab.recognize(str)) {
System.out.println("String is valid");
} else {
System.out.print("String is Invalid");
}
}
}