Talentd
FleetCode

1433. Check If a String Can Break Another String

Medium70.9% acceptance

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.

Example 1:

Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".

Example 2:

Input: s1 = "abe", s2 = "acd"
Output: false 
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.

Example 3:

Input: s1 = "leetcodee", s2 = "interview"
Output: true

Constraints:

  • s1.length == n
  • s2.length == n
  • 1 <= n <= 10^5
  • All strings consist of lowercase English letters.

Hints open one at a time

Hint 1
Sort both strings and then check if one of them can break the other.
8:21
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def checkIfCanBreak(self, s1: str, s2: str) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
"abc"
"xya"
Input
"abe"
"acd"
Input
"leetcodee"
"interview"
Your code is saved on this device.Solution