↓Skip to main content
  1. LeetCode/

LeetCode 551: Student Attendance Record I

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-27

  • Submissions: Wrong Answer → Accepted ×4

Approach
#

Two conditions: fewer than two 'A's in total, and no three consecutive 'L's, i.e. "LLL" not in s (String Template 2).

My Attempts
#

Attempt 1 wrote for i in s: if i[:3] == "LLL"; i is a single character, so i[:3] is never "LLL": "LLL" returned True. Using s[i:i + 3] passed.

Solution
#

class Solution:
    def checkRecord(self, s: str) -> bool:
        return s.count("A") < 2 and "LLL" not in s

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Consecutive Ls need a substring check; a per-character loop variable is a single character.

Interview Explanation
#

I check that there are fewer than two A’s and that LLL does not occur, in O(n).

Related

LeetCode 504: Base 7
·1 min
LeetCode Easy Math String
LeetCode 504 note: repeatedly take % 7 and // 7 of the absolute value, then add the sign and reverse; handle 0 separately
LeetCode 541: Reverse String II
·1 min
LeetCode Easy Two-Pointers String
LeetCode 541 note: in every block of 2k reverse the first k; when fewer than k remain reverse them all, which slicing handles automatically
LeetCode 1446: Consecutive Characters
·1 min
LeetCode Easy String
LeetCode 1446 note: track the current run of equal characters in one pass, resetting to 1 on a change
LeetCode 1869: Longer Contiguous Segments of Ones than Zeros
·1 min
LeetCode Easy String
LeetCode 1869 note: find the longest run of 1s and of 0s and compare; split does it in one line
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 459: Repeated Substring Pattern
·1 min
LeetCode Easy String String-Matching Z-Algorithm Knuth-Morris-Pratt-Algorithm
LeetCode 459 note: s is a repetition of a substring exactly when s occurs in s + s with its first and last characters removed