package test Created by GrabetsB on 24 02 2015 public class LanguageDu

 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
package test;
/**
* Created by GrabetsB on 24.02.2015.
*/
public class LanguageDuke
{
static int count = 0;
static boolean result = false;
public static boolean recognize(String word){
if (word.charAt(0)==')') return false;
return recognize(word, 0);
}
public static boolean recognize(String word, int at){
if (word.length()-1<at) return result;
else {
char c = word.charAt(at);
if ( c == '(')
{
count++;
recognize(word, at + 1);
}
else if (c==')'){
count--;
recognize(word, at+1);
}
if (count==0) result =true;
else result = false;
}
return result;
}
public static void main(String[] args)
{
System.out.println(recognize(")"));
System.out.println(recognize("()"));
System.out.println(recognize("((()()))"));
System.out.println(recognize("()()()()("));
}
}