Skip to content

Commit

Permalink
Merge pull request #225 from ben1009/551
Browse files Browse the repository at this point in the history
feat: 551
  • Loading branch information
mergify[bot] authored Aug 19, 2024
2 parents 9d8767d + deee23a commit 3f11e7f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/problem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub mod p0496_next_greater_element_i;
pub mod p0503_next_greater_element_ii;
pub mod p0521_longest_uncommon_subsequence;
pub mod p0543_diameter_of_binary_tree;
pub mod p0551_student_attendance_record_i;
pub mod p0560_subarray_sum_equals_k;
pub mod p0581_shortest_unsorted_continuous_subarray;
pub mod p0617_merge_two_binary_trees;
Expand Down
72 changes: 72 additions & 0 deletions src/problem/p0551_student_attendance_record_i.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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'.

// https://leetcode.com/problems/student-attendance-record-i/

pub struct Solution {}

impl Solution {
pub fn check_record(s: String) -> bool {
if s.len() < 2 {
return true;
}

let mut a = 0;
let mut l = 0;
let s = s.as_bytes();
for i in 0..s.len() {
if s[i] == b'L' {
if i == 0 || s[i - 1] != b'L' {
l = 1;
} else {
l += 1;
if l == 3 {
return false;
}
}
} else if s[i] == b'A' {
a += 1;
}
}

a < 2
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_0551() {
assert!(Solution::check_record("PPALLP".to_string()));
assert!(!Solution::check_record("PPALLL".to_string()));
}
}

0 comments on commit 3f11e7f

Please sign in to comment.