-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTwo substring
More file actions
53 lines (44 loc) · 1.16 KB
/
Two substring
File metadata and controls
53 lines (44 loc) · 1.16 KB
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
44
45
46
47
48
49
50
51
52
53
code in java *****************************************
public class Solution {
public static void twoSubstrings(String s) {
boolean f1=false,s1=false;
int i;
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)=='A' && s.charAt(i+1)=='B')
{
i+=2;f1=true;
break;
}
}
for(int j=i;j<s.length()-1;j++)
{
if(s.charAt(j)=='B' && s.charAt(j+1)=='A')
{
s1=true;
break;
}
}
boolean f2=false,s2=false;
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)=='B' && s.charAt(i+1)=='A')
{
i+=2;s2=true;
break;
}
}
for(int j=i;j<s.length()-1;j++)
{
if(s.charAt(j)=='A' && s.charAt(j+1)=='B')
{
f2=true;
break;
}
}
if((f1 && s1) || (f2 && s2))
System.out.println("YES");
else
System.out.println("NO");
}
}