给定一篇由大写字母、小写字母、逗号、句号组成的文章,求使文章不合法的字母数。
文章不合法有2种情况:
1.句子的第一个字母用了小写。
2.不是单词的首字母用了大写。
样例
样例1
输入: s=“This won iz correkt. It has, No Mistakes et Oll. But there are two BIG mistakes in this sentence. and here is one more.”
输出: 3
解释:
‘BIG’ 中’I’和’‘G’,以及最后一句话中的第一个字母不合法.
样例2
输入: s=“Hahaha. HahaHa. hahahah.”
输出: 2
解释:
‘HahaHa’ 中的第二个’H’和最后一个单词的第一个’h’不合法.
注意事项
一个句子当且仅当句号时结束。
文章的总长度不超过
1000010000
10000
public static int count(String s) {
// Write your code here.
int num =0;
int i=0;
boolean st=false;
if(s.length()<=0)return 0;
if(s.charAt(0)>='a'&&s.charAt(0)<='z')num++;
for(i=1;i<s.length();i++){
char a=s.charAt(i);
char b=s.charAt(i-1);
//判断是否为句首
if(b=='.')st=true;
if(st&&a>='a'&&a<='z')num++;//句子的第一个字母用了小写。
if((a>='a'&&a<='z')||(a >= 'A'&&a<= 'Z'))//如果不是句子开头,就把这个变量置为false
st = false;
if((b!=' '&&b!='.'&&b!=',')&&(a>='A'&&a<='Z'))num++;//不是单词的首字母用了大写。
}
return num;
}