原文链接: https://leetcode-cn.com/problems/student-attendance-record-i
英文原文
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 <= 1000s[i]is either'A','L', or'P'.
中文题目
给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
'A':Absent,缺勤'L':Late,迟到'P':Present,到场
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
- 按 总出勤 计,学生缺勤(
'A')严格 少于两天。 - 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到(
'L')记录。
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
示例 1:
输入:s = "PPALLP" 输出:true 解释:学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。
示例 2:
输入:s = "PPALLL" 输出:false 解释:学生最后三天连续迟到,所以不满足出勤奖励的条件。
提示:
1 <= s.length <= 1000s[i]为'A'、'L'或'P'
通过代码
高赞题解
模拟
放大假,根据题意进行模拟即可。
代码:
[]class Solution { public boolean checkRecord(String s) { int n = s.length(); char[] cs = s.toCharArray(); for (int i = 0, cnt = 0; i < n; ) { char c = cs[i]; if (c == 'A') { cnt++; if (cnt >= 2) return false; } else if (c == 'L') { int j = i; while (j < n && cs[j] == 'L') j++; int len = j - i; if (len >= 3) return false; i = j; continue; } i++; } return true; } }
- 时间复杂度:$O(n)$
- 空间复杂度:使用
charAt代替toCharArray的话为 $O(1)$,否则为 $O(n)$
其他「模拟」内容
今天每日一题不够做吗?贴心的为大家准备了以下内容 🤣
注:以上目录整理来自 wiki,任何形式的转载引用请保留出处。
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 60492 | 106576 | 56.8% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|
相似题目
| 题目 | 难度 |
|---|---|
| 学生出勤记录 II | 困难 |