← Home

1433. Check If a String Can Break Another String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
143

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1433. Check If a String Can Break Another String, the solution in this repository is mainly a greedy solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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: greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are checkIfCanBreak, counter1.

Guide

Why?

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

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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//Runtime: 812 ms, faster than 20.00% of C++ online submissions for Check If a String Can Break Another String.
02//Memory Usage: 11.6 MB, less than 100.00% of C++ online submissions for Check If a String Can Break Another String.
03class Solution {
04public:
05    bool checkIfCanBreak(string s1, string s2) {
06        int n = s1.size();
07        
08        sort(s1.begin(), s1.end());
09        sort(s2.begin(), s2.end());
10        
11        bool ab = true, ba = true;
12        
13        for(int i = 0; i < n; i++){
14            if(s1[i] < s2[i]) ab = false;
15            if(s2[i] < s1[i]) ba = false;
16        }
17        
18        return ab || ba;
19    }
20};
21
22//O(n) solution, using map, from definition of "break"
23//https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/608668/Python-O(n).
24//Runtime: 84 ms, faster than 100.00% of C++ online submissions for Check If a String Can Break Another String.
25//Memory Usage: 11.8 MB, less than 100.00% of C++ online submissions for Check If a String Can Break Another String.
26class Solution {
27public:
28    bool checkIfCanBreak(string s1, string s2) {
29        int n = s1.size();
30        vector<int> counter1(26, 0), counter2(26, 0);
31        for(int i = 0; i < n; i++){
32            counter1[s1[i]-'a']++;
33            counter2[s2[i]-'a']++;
34        }
35        
36        int diff1 = 0, diff2 = 0;
37        bool break1 = true, break2 = true;
38        for(int i = 0; i < 26; i++){
39            diff1 += counter1[i] - counter2[i];
40            diff2 += counter2[i] - counter1[i];
41            if(diff1 < 0) break1 = false;
42            if(diff2 < 0) break2 = false;
43        }
44        
45        return break1 || break2;
46    }
47};

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.