Java 1-liner


  • 32
    public boolean checkRecord(String s) {
        return !s.matches(".*LLL.*|.*A.*A.*");
    }
    

  • 1

    Thanks for your solution.That's quite a simple and creative method.


  • 0
    M

    @compton_scatter Holy cow !!!! amazing.


  • 2
    G

    @compton_scatter yes,you use regex,that's ok, this is my 1-line code in python:

     return s.count('A')<2 and s.count('LLL')==0
    

  • 0
    I

    @compton_scatter
    Of course regex is a smart way but a little tricky, I use iteration and flag/counters to implement this problem, and surprisingly my method is faster than regex.

    class Solution {
        public boolean checkRecord(String s) {
            // return !s.matches(".*LLL.*|.*A.*A.*");
            boolean oneA = false;
            boolean oneL = false;
            boolean twoL = false;
            for(char c : s.toCharArray()) {
                if(c != 'L') {
                    oneL = false;
                    twoL = false;
                } else {
                    if(oneL && twoL)
                        return false;
                    else if(!oneL)
                        oneL = true;
                    else
                        twoL = true;
                }
                if(c == 'A') {
                    if(oneA)
                        return false;
                    else
                        oneA = true;
                }
            }
            return true;
        }
    }
    

  • 0
    P

    楼主牛逼!,66666666666666666!!!!


  • 0
    V

    Another simple solution :

    class Solution {
        public boolean checkRecord(String s) 
        {
        		if (s.contains("LLL")) return false;
        		
        		String str = s.replaceAll("A", "");
        		
        		if ((s.length() - str.length()) > 1)
        		{
        			return false;
        		}
        		
        		
            return true;
            
        }
    }
    
    

  • 0
    Y

    I guess this question is unreasonable.If my record is LLAPP, I can get reward...But LLLPP no reward..That is to say absent is better than late.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.