- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 48 lines of Python from the credited upstream file 420.py.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def strongPasswordChecker(self, password: str) -> int:3 n = len(password)4 missing = self._getMissing(password)5 6 replaces = 07 8 9 oneSeq = 010 11 12 twoSeq = 013 14 i = 215 while i < n:16 if password[i] == password[i - 1] and password[i - 1] == password[i - 2]:17 length = 2 18 while i < n and password[i] == password[i - 1]:19 length += 120 i += 121 replaces += length 3 22 if length % 3 == 0:23 oneSeq += 124 if length % 3 == 1:25 twoSeq += 126 else:27 i += 128 29 if n < 6:30 return max(6 - n, missing)31 if n <= 20:32 return max(replaces, missing)33 34 deletes = n - 2035 36 replaces -= min(oneSeq, deletes)37 38 replaces -= min(max(deletes - oneSeq, 0), twoSeq * 2) 239 40 replaces -= max(deletes - oneSeq - twoSeq * 2, 0) 341 return deletes + max(replaces, missing)42 43 def _getMissing(self, password: str) -> int:44 return (345 - any(c.isupper() for c in password)46 - any(c.islower() for c in password)47 - any(c.isdigit() for c in password))48