资讯专栏INFORMATION COLUMN

[LeetCode] Student Attendance Record I

RancherLabs / 1054人阅读

Problem

You are given a string representing an attendance record for a student. The record only contains the following three characters:
"A" : Absent.
"L" : Late.
"P" : Present.
A student could be rewarded if his attendance record doesn"t contain more than one "A" (absent) or more than two continuous "L" (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

Solution
class Solution {
    public boolean checkRecord(String s) {
        if (s == null || s.length() == 0) return true;
        Map map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!map.containsKey(ch)) map.put(ch, 1);
            else map.put(ch, map.get(ch)+1);
            if (ch == "A" && map.get(ch) > 1) return false;
            if (i+3 <= s.length() && s.substring(i, i+3).equals("LLL")) {
                return false; 
            }
        }
        return true;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77140.html

相关文章

  • JavaEE环境配置与示例教程

    摘要:环境配置运行环境安装配置数据库下载安装下载地址牢记安装过程中设置的用户的密码安装选择版本的安装配置数据库驱动教程前提开发环境参考环境配置文档基础知识基本语法协议基础知识只需了解请求即可基础的等。 **寒假的时候老师让写个简单的JavaEE教程给学弟or学妹看,于是写了下面的内容。发表到这个地方以防丢失。。。因为写的时候用的是word,直接复制过来格式有点乱。。。所以不要在意细节了。。...

    AbnerMing 评论0 收藏0
  • [LeetCode] 763. Partition Labels

    Problem A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers represe...

    iliyaku 评论0 收藏0
  • 【硬核】用C语言来写学生成绩管理系统,让你的课设不再是难题

    摘要:嗨这里是狐狸大家的期末课设要来了吧,有想法做什么了嘛,有没有为此熬夜,有没有为此努力呢,今天,我们来写一个学生成绩管理系统,一方面是让大家复习一下自己学过的知识,一方面是为了给大家的期末课设提供一点思路。 目录 序 嗨!这里是狐狸~~ 一、需求分析说明 二、概要设计说明 三、详细设计说明 1...

    seanHai 评论0 收藏0
  • [LeetCode] Intersection of Two Arrays I & II

    Intersection of Two Arrays I Problem Given two arrays, write a function to compute their intersection. Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note Each element in the result m...

    lucas 评论0 收藏0
  • LeetCode——Longest Substring Without Repeating Char

    摘要:原问题我的沙雕解法无重复字母存在重复字母挨打最暴力的无脑解法,耗时。。。 原问题 Given a string, find the length of the longest substring without repeating characters. Example 1: Input: abcabcbb Output: 3 Explanation: The answer is a...

    forsigner 评论0 收藏0

发表评论

0条评论

RancherLabs

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<