Leetcode: Attendance record for students | "551"

Source: Internet
Author: User

tag: PPA int resolves the problem of emptying a matching regular expression loop string

Leetcode: Attendance record of Students | "551" topic description

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

    1. ' A ': Absent, absence
    2. ' L ': late, late
    3. ' P ': Present, on the scene

If a student's attendance record does not exceed a ' a ' (absence) and does not exceed two consecutive ' L ' (late), then the student will be rewarded.

You need to judge whether he will be rewarded according to the student's attendance record.

Example 1:

Input: "PPALLP" output: True

Example 2:

Input: "ppalll" output: False
Problem analysis

For string problems, we need to reflect on the so-called rock-regular expression .

Many problems can be solved quickly, because we are suffering too little, so it backfired.

  l{3,} can represent 3 consecutive and above L appear, a.*a can represent 2 times a , so if the regular match is successful, then it will not be rewarded!

Java
Class Solution {public    Boolean Checkrecord (String s)    {        if (s.matches (". *l{3,}.*"))            return false;        if (S.matches (". *a.*a.*"))            return false;        return true;    }}

But the above for This problem, the Regular expression solution is concise but not efficient .

We loop through the string once, a variable counts the number of times a, a variable counts the number of consecutive L times, there is a technique for successive times, when the first L is encountered, the next is not L, and the count variable is emptied immediately .

public Boolean Checkrecord (String s) {        int a = 0;        int l = 0;        for (char C:s.tochararray ())        {            if (c== ' A ')                a++;            if (c== ' L ')                l++;            else                l=0;            if (a>1| | l>=3)                return false;        }        return true;    }

  

Leetcode: Attendance record for students | "551"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.