pair match

01.import java.util.Scanner;

02.import java.util.Stack;

03.

04.public class Main{

05.static Stack<Character> str=new Stack<Character>();

06.public static boolean match(String s)

07.{

08.//str.push(s.charAt(0));

09.char ch;

10.boolean flag=true;

11.for(int i=0;i<s.length();i++)

12.{

13.ch=s.charAt(i);

14.if(str.isEmpty())

15.{

16.str.push(ch);

17.continue;

18.}

19.if((ch==']'&&str.peek()=='[')||(ch==')'&&str.peek()=='('))

20.{

21.str.pop();

22.}

23.else {

24.str.push(ch);

25.}

26.}

27.if(!str.isEmpty()){

28.flag=false;

29.str.clear();

30.}

31.return flag;

32.}

33.public static void main(String[] args) {

34.Scanner in=new Scanner(System.in);

35.int n=in.nextInt();

36.while(n--!=0){

37.String s=in.next();

38.if(s.length()%2==1 || s.charAt(0)==']' || s.charAt(0)==')')

39.{

40.System.out.println("No");

41.continue;

42.}

43.if(match(s)){

44.System.out.println("Yes");

45.}else{

46.System.out.println("No");

47.}

48.}

49.

50.}

51.

52.}

 
01.import java.util.Scanner;
02.import java.util.Stack;
03. 
04.public class Main{
05.static Stack<Character> str=new Stack<Character>();
06.public static boolean match(String s)
07.{
08.//str.push(s.charAt(0));
09.char ch;
10.boolean flag=true;
11.for(int i=0;i<s.length();i++)
12.{
13.ch=s.charAt(i);
14.if(str.isEmpty())
15.{
16.str.push(ch);
17.continue;
18.}
19.if((ch==']'&&str.peek()=='[')||(ch==')'&&str.peek()=='('))
20.{
21.str.pop();
22.}
23.else {
24.str.push(ch);
25.}
26.}
27.if(!str.isEmpty()){
28.flag=false;
29.str.clear();
30.}
31.return flag;
32.}
33.public static void main(String[] args) {
34.Scanner in=new Scanner(System.in);
35.int n=in.nextInt();
36.while(n--!=0){
37.String s=in.next();
38.if(s.length()%2==1 || s.charAt(0)==']' || s.charAt(0)==')')
39.{
40.System.out.println("No");
41.continue;
42.}
43.if(match(s)){
44.System.out.println("Yes");
45.}else{
46.System.out.println("No");
47.}
48.}
49. 
50.}
51. 
52.}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s