2017-08-12 17:16:42
1楼#1层
Python:
class Solution(object):
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
return s.count('A') <=1 and 'LLL' not in s
2017-08-12 17:17:09
2楼#1层
C++:
class Solution {
public:
bool checkRecord(string s) {
int abnum=0;
int latenum=0;
int lastnum=0;
for (int i=0; i<s.size(); i++)
{
if (s[i]=='A')
{
abnum++;
lastnum=max(lastnum,latenum);
latenum=0;
}
else if (s[i]=='L')
{
latenum++;
}
else
{
lastnum=max(lastnum,latenum);
latenum=0;
}
}
lastnum=max(lastnum,latenum);
if (abnum>1||lastnum>2)
return false;
return true;
}
};
2017-08-12 17:17:40
3楼#1层
Java:
public class Solution {
public boolean checkRecord(String s) {
int a = 0;
int l = 0;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (c == 'P') {
l = 0;
continue;
} else if (c == 'A') {
l = 0;
a++;
if (a > 1)
return false;
} else {
l++;
if (l > 2)
return false;
}
}
return true;
}
}