I'm posting my code for a LeetCode problem copied here. If you have time and would like to golf for a short solution, please do so.
It has to only pass the LeetCode's Online Judge, if the language would be available.
If the language is not available, simply we should possibly keep the
class Solutionas well as the function name.
Languages and Version Notes
Available languages are:
- C++ clang 9 Uses the C++17 standard.
- Java 13.0.1
- Python 2.7.12
- Python3 3.8
- C gcc 8.2
- C# 8.0
- Ruby 2.4.5
- Bash 4.3.30
- Swift 5.0.1
- Go 1.13
- Scala 2.13
- Kotlin 1.3.10
- Rust 1.40.0
- PHP 7.2
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 10 ^ 9 + 7.
A student attendance record is a string that only contains the following three characters:
- 'A' : Absent.
- 'L' : Late.
- 'P' : Present.
A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
- Input: n = 2
- Output: 8
There are 8 records with length 2 will be regarded as rewardable:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" won't be regarded as rewardable owing to more than one absent times.
Note: The value of n won't exceed 100,000.
class Solution: def checkRecord(self, n): MOD = 1000000007 a = b = d = 1 c = e = f = 0 for _ in range(n - 1): a, b, c, d, e, f = (a + b + c) % MOD, a, b, (a + b + c + d + e + f) % MOD, d, e return (a + b + c + d + e + f) % MOD
Accepted Golf Attempt in Python 3 - 185 bytes - I
class Solution: def checkRecord(self,n): M=1000000007;a=b=d=1;c=e=f=0 for _ in range(n-1):a,b,c,d,e,f=(a+b+c)%M,a,b,(a+b+c+d+e+f)%M,d,e return(a+b+c+d+e+f)%M
Accepted Golf Attempt in Python 3 - 185 bytes - II
class Solution: def checkRecord(self,n): M=int(1e9+7);a=b=d=1;c=e=f=0 for _ in range(n-1):a,b,c,d,e,f=(a+b+c)%M,a,b,(a+b+c+d+e+f)%M,d,e return(a+b+c+d+e+f)%M
In LeetCode's answer template, there is a class usually named
Solution with one or more
public functions which we are not allowed to rename.