본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 551. Student Attendance Record I

반응형

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

 

Student Attendance Record I - LeetCode

Can you solve this real interview question? 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

leetcode.com

구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

'A'개수를 셀 aCnt와 'L'개수를 셀 lCnt를 선언 후 0으로 초기화합니다.

📔 풀이과정

s에 대해 for loop를 수행합니다.

1. aCnt가 2거나 lCnt가 3이라면 자격이 없습니다. break해줍니다. 

2. 'A'와 'L'인 경우에 따라 aCnt 또는 lCnt를 세줍니다. 'L'이 아닌 경우라면 연속되지 않았으므로 lCnt를 0으로 초기화해줘야 합니다.

📔 정답 출력 | 반환

자격이 있으면 true 아니라면 false를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    bool checkRecord(string s) {
        int aCnt = 0, lCnt = 0;
        for(auto c : s) {
            if(aCnt == 2 || lCnt == 3) break;
            if(c == 'A') aCnt++;
            if(c == 'L') lCnt++;
            else {
                lCnt = 0;
            }
        }
        return aCnt < 2 && lCnt < 3;
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.