#1433
Medium Algorithms Check if a string can break another string
String Greedy Sorting
70.9% acceptance
Feb 25, 2026
781
155
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa. In other words s2 can break s1 or vice-versa.
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn check_if_can_break(s1: String, s2: String) -> bool {
let mut a: Vec<char> = s1.chars().collect();
let mut b: Vec<char> = s2.chars().collect();
a.sort_unstable();
b.sort_unstable();
let a_breaks_b = a.iter().zip(b.iter()).all(|(x, y)| x >= y);
let b_breaks_a = b.iter().zip(a.iter()).all(|(x, y)| x >= y);
a_breaks_b || b_breaks_a
}
}