{"id":3568,"date":"2018-08-17T03:02:09","date_gmt":"2018-08-17T10:02:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3568"},"modified":"2018-08-17T03:03:09","modified_gmt":"2018-08-17T10:03:09","slug":"leetcode-551-student-attendance-record-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-551-student-attendance-record-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 551. Student Attendance Record I"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 551. Student Attendance Record I - \u5237\u9898\u627e\u5de5\u4f5c EP11\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/fZ8nkk220M4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>You are given a string representing an attendance record for a student. The record only contains the following three characters:<\/p>\n<ol>\n<li><b>&#8216;A&#8217;<\/b>\u00a0: Absent.<\/li>\n<li><b>&#8216;L&#8217;<\/b>\u00a0: Late.<\/li>\n<li><b>&#8216;P&#8217;<\/b>\u00a0: Present.<\/li>\n<\/ol>\n<p>A student could be rewarded if his attendance record doesn&#8217;t contain\u00a0<b>more than one &#8216;A&#8217; (absent)<\/b>\u00a0or\u00a0<b>more than two continuous &#8216;L&#8217; (late)<\/b>.<\/p>\n<p>You need to return whether the student could be rewarded according to his attendance record.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> \"PPALLP\"\r\n<b>Output:<\/b> True\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> \"PPALLL\"\r\n<b>Output:<\/b> False<\/pre>\n<h1><strong>Solution1: Simulation<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n  bool checkRecord(string s) {\r\n    int a{0};\r\n    int l{0};\r\n    for(char c: s) {\r\n        if (c == 'A') ++a;\r\n        if (c == 'L') ++l;\r\n        else l=0;\r\n        if (a &gt; 1 || l &gt; 2) return false;\r\n    }\r\n    return true;\r\n  }\r\n};<\/pre>\n<h1>Solution 2: Regex<\/h1>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n  bool checkRecord(string s) {\r\n    return !std::regex_search(s, std::regex(\"A.*A|LLL\"));\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You are given a string representing an attendance record for a student. The record only contains the following three characters: &#8216;A&#8217;\u00a0: Absent. &#8216;L&#8217;\u00a0: Late.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,47],"tags":[224,179,4],"class_list":["post-3568","post","type-post","status-publish","format-standard","hentry","category-simulation","category-string","tag-regex","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3568"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3568\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3568\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}