Skip to content

1433. Check If a String Can Break Another String 👍

Approach 1: Count

  • Time: $O(n)$
  • Space: $O(26) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
 public:
  bool checkIfCanBreak(string s1, string s2) {
    vector<int> count1(26);
    vector<int> count2(26);

    for (const char c : s1)
      ++count1[c - 'a'];

    for (const char c : s2)
      ++count2[c - 'a'];

    return canBreak(count1, count2) || canBreak(count2, count1);
  }

 private:
  // Returns true if count1 can break count2.
  bool canBreak(const vector<int>& count1, const vector<int>& count2) {
    int diff = 0;
    for (int i = 0; i < 26; ++i) {
      diff += count2[i] - count1[i];
      // count2 is alphabetically greater than count1.
      if (diff < 0)
        return false;
    }
    return true;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
  public boolean checkIfCanBreak(String s1, String s2) {
    int[] count1 = new int[26];
    int[] count2 = new int[26];

    for (final char c : s1.toCharArray())
      ++count1[c - 'a'];

    for (final char c : s2.toCharArray())
      ++count2[c - 'a'];

    return canBreak(count1, count2) || canBreak(count2, count1);
  }

  // Returns true if count1 can break count2.
  private boolean canBreak(int[] count1, int[] count2) {
    int diff = 0;
    for (int i = 0; i < 26; ++i) {
      diff += count2[i] - count1[i];
      // count2 is alphabetically greater than count1.
      if (diff < 0)
        return false;
    }
    return true;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
  def checkIfCanBreak(self, s1: str, s2: str) -> bool:
    count1 = collections.Counter(s1)
    count2 = collections.Counter(s2)

    def canBreak(count1: dict[str, int], count2: dict[str, int]) -> bool:
      """Returns True if count1 can break count2."""
      diff = 0
      for c in string.ascii_lowercase:
        diff += count2[c] - count1[c]
        # count2 is alphabetically greater than count1.
        if diff < 0:
          return False
      return True

    return canBreak(count1, count2) or canBreak(count2, count1)

Approach 2: For Fun

  • Time: $O(n)$
  • Space: $O(26) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  bool checkIfCanBreak(string s1, string s2) {
    vector<int> count(26);

    for (const char c : s1)
      ++count[c - 'a'];

    for (const char c : s2)
      --count[c - 'a'];

    for (int i = 1; i < 26; ++i)
      count[i] += count[i - 1];

    return ranges::all_of(count, [](int c) { return c <= 0; }) ||
           ranges::all_of(count, [](int c) { return c >= 0; });
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
  public boolean checkIfCanBreak(String s1, String s2) {
    int[] count = new int[26];

    for (final char c : s1.toCharArray())
      ++count[c - 'a'];

    for (final char c : s2.toCharArray())
      --count[c - 'a'];

    for (int i = 1; i < 26; ++i)
      count[i] += count[i - 1];

    return Arrays.stream(count).allMatch(c -> c <= 0) || Arrays.stream(count).allMatch(c -> c >= 0);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def checkIfCanBreak(self, s1: str, s2: str) -> bool:
    count = collections.Counter(s1)
    count.subtract(collections.Counter(s2))

    for a, b in itertools.pairwise(string.ascii_lowercase):
      count[b] += count[a]

    return (all(value <= 0 for value in count.values()) or
            all(value >= 0 for value in count.values()))