Tiny Ruby, Short Python/Java/C++


  • 5

    Just check that there aren't two 'A' or three consecutive 'L' (takes care of all "more than two" cases). Easiest with a regular expression, though at least Python and Ruby also have nice "count A" and "contains LLL" functionality.

    Ruby:

    def check_record(s)
      !s[/A.*A|LLL/]
    end
    

    or

    def check_record(s)
      !(s.count("A") > 1 || s.include?("LLL"))
    end
    

    Python:

    def checkRecord(self, s):
        return not re.search('A.*A|LLL', s)
    

    or

    def checkRecord(self, s):
        return not (s.count('A') > 1 or 'LLL' in s)
    

    Java:

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

    C++:

    bool checkRecord(string s) {
        return !regex_search(s, regex("A.*A|LLL"));
    }

  • -2
    Z

    @StefanPochmann this python method, a little slow


Log in to reply
 

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