-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularSentence.java
More file actions
30 lines (28 loc) · 867 Bytes
/
CircularSentence.java
File metadata and controls
30 lines (28 loc) · 867 Bytes
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
/**
* CircularSentence
*/
public class CircularSentence {
public static void main(String[] args) {
String s="leetcode exercises sound delightful";
System.out.println(isCircularSentence(s));
}
public static boolean isCircularSentence(String sentence) {
int n=sentence.length()-1;
if(sentence.charAt(0)!=sentence.charAt(n)){
return false;
}
sentence+=" ";
char last=sentence.charAt(0);
for(int i=0;i<sentence.length()-1;i++){
if(sentence.charAt(i)!=' ' && sentence.charAt(i+1)==' '){
last=sentence.charAt(i);
}
if(sentence.charAt(i)==' ' && sentence.charAt(i+1)!=' '){
if(last!=sentence.charAt(i+1)){
return false;
}
}
}
return true;
}
}