← Home

420. Strong Password Checker

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
420

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 420. Strong Password Checker, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • https://leetcode.com/problems/strong-password-checker/discuss/91008/Simple-Python-solution

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are strongPasswordChecker.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/strong-password-checker/discuss/91008/Simple-Python-solution
02//Runtime: 4 ms, faster than 28.21% of C++ online submissions for Strong Password Checker.
03//Memory Usage: 6.1 MB, less than 58.62% of C++ online submissions for Strong Password Checker.
04class Solution {
05public:
06    int strongPasswordChecker(string s) {
07        int n = s.size();
08        
09        int missing_type = 3;
10        
11        if(any_of(s.begin(), s.end(), [](char& a){return a >= 'a' && a <= 'z';})){
12            --missing_type;
13        }
14        if(any_of(s.begin(), s.end(), [](char& a){return a >= 'A' && a <= 'Z';})){
15            --missing_type;
16        }
17        if(any_of(s.begin(), s.end(), [](char& a){return a >= '0' && a <= '9';})){
18            --missing_type;
19        }
20        
21        // cout << "missing_type: " << missing_type << endl;
22        
23        int change = 0;
24        int one = 0, two = 0;
25        int pos = 2;
26        
27        while(pos < n){
28            int len = 0;
29            if(s[pos-2] == s[pos-1] && s[pos-1] == s[pos]){
30                len = 3;
31                
32                while(pos+1 < n && s[pos] == s[pos+1]){
33                    ++len;
34                    ++pos;
35                }
36                
37                if(len%3 == 0){
38                    //delete one char, so need one less changes
39                    ++one;
40                }else if(len%3 == 1){
41                    //reduce one changes by deleting two chars
42                    ++two;
43                }
44                /*
45                assume repeating chars' length is 3's multiple + 2,
46                need len/3 changes
47                */
48                change += len/3;  
49            }
50            ++pos;
51        }
52        
53        if(n < 6){
54            //need 6-n insertion or missing_type replacement/insertion
55            /*
56            if missing_type = 0:
57            "aaaA1", insert one char to break "aaa"
58            if missing_type = 1:
59            "aaaaA", insert one char to break "aaaa"
60            if missing_type = 2:
61            "aaaaa", replace one "a" and insert one char
62            */
63            return max(missing_type, 6-n);
64        }else if(n <= 20){
65            return max(missing_type, change);
66        }else{
67            /*
68            this is the quote of chars to be deleted,
69            they are not yet actually deleted!
70            */
71            int del = n - 20;
72            int del_quota = del;
73            
74            /*
75            we have "del_quota" quota chars to delete,
76            and here we choose the chars corresponding to "one" for deleting
77            */
78            change -= min(del_quota, one);
79            //decrease the quota
80            del_quota = max(del_quota-one, 0);
81            
82            /*
83            choosing "two"*2 chars for deleting,
84            so we need two*2 less "change"
85            */
86            change -= min(del_quota, two*2) / 2;
87            del_quota = max(del_quota - 2*two, 0);
88            
89            /*
90            replace the change with delete
91            */
92            change -= del_quota/3;
93                
94            return del + max(missing_type, change);
95        }
96    }
97};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.