551, Student Attendance Record I

동청·2022년 8월 17일
0

leetcode

목록 보기
20/39

Problem

leetcode 바로가기

You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

  • 'A': Absent.
  • 'L': Late.
  • 'P': Present.

The student is eligible for an attendance award if they meet both of the following criteria:

  • The student was absent ('A') for strictly fewer than 2 days total.
  • The student was never late ('L') for 3 or more consecutive days.

Return true if the student is eligible for an attendance award, or false otherwise.

Example 1:

Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.

Example 2:

Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either 'A', 'L', or 'P'.

Solution

/**
 * @param {string} s
 * @return {boolean}
 */
var checkRecord = function(s) {
    if (s.length < 2) {
        return true;
    }
  let num = [0];

  for (let i = 0; i < s.length; i++) {
    if(s[i] == "A") {
      num++;
      if (num == 2) {
        return false;
      }
    } else if (s[i - 2] == "L" && s[i - 1] == "L" && s[i] == "L") {
      return false;
    }
  }

  return true;
};

2개의 댓글

comment-user-thumbnail
2023년 10월 4일

Introducing the all-new Microsoft https://www.dumpsmate.com/MS-102-exam.html Exam Prep [2023] – your key to unlocking career opportunities! Our comprehensive study materials are meticulously designed to align with the latest exam objectives. Prepare with confidence as we cover the freshest set of questions and scenarios, ensuring you're fully equipped to ace the MS-102 Exam. Stay ahead of the curve with our up-to-date content, expertly crafted to enhance your skills in Microsoft 365 Identity and Services. Don't miss out on this opportunity to excel in your career – get started today with the most cutting-edge MS-102 Exam Prep available! Your success is just a click away.

답글 달기
comment-user-thumbnail
2023년 12월 4일

I like being a student, although there are difficult periods in this. Especially when you need to prepare for assignments and exams. This service help with nursing essay. And this always helps me out in difficult moments and helps me prepare properly.

답글 달기