leetCode551. Student Attendance Record I

Given a string representing a student's attendance record, the record contains only the following three characters:

  1. 'A'  : Absent, absent
  2. 'L'  : Late, late
  3. 'P'  : Present

A student will be rewarded if there is no more than one 'A' (absence) and no more than two consecutive 'L' (late) in his attendance record .

You need to determine whether the student will be rewarded based on his attendance record.

Example 1:

Input: "PPALLP"
 Output: True

Example 2:

Input: "PPALLL"
 Output: False

Have you encountered this question in a real interview session?  

This question is very simple, it is a game of words. . .

class Solution {
    public boolean checkRecord(String s) {
       int Acount=0,Lcount=0;
        for (int i = 0; i < s.length(); i++) {
			if(s.charAt(i)=='A') {
				Acount++;
			}
		}
        for (int i = 0; i < s.length()-2; i++) {
			if(s.charAt(i)=='L'&&s.charAt(i+1)=='L'&&s.charAt(i+2)=='L') {
				Lcount++;
			}
		}
        return Acount<=1&&Lcount<1;
    }
}
class Solution {
    public boolean checkRecord(String s) {
       int Acount=0,Lcount=0;
        for (int i = 0; i < s.length(); i++) {
			if(s.charAt(i)=='A') {
				Acount++;
			}
		}
        for (int i = 0; i < s.length()-2; i++) {
			if(s.charAt(i)=='L'&&s.charAt(i+1)=='L'&&s.charAt(i+2)=='L') {
				Lcount++;
			}
		}
        return Acount<=1&&Lcount<1;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837509&siteId=291194637