给你两个字符串 s1 和 s2 ,它们长度相等,请你检查是否存在一个 s1 的排列可以打破 s2 的一个排列,或者是否存在一个 s2 的排列可以打破 s1 的一个排列。
字符串 x 可以打破字符串 y (两者长度都为 n )需满足对于所有 i(在 0 到 n - 1 之间)都有 x[i] >= y[i](字典序意义下的顺序)。
示例 1:
输入:s1 = "abc", s2 = "xya"
输出:true
解释:"ayx" 是 s2="xya" 的一个排列,"abc" 是字符串 s1="abc" 的一个排列,且 "ayx" 可以打破 "abc" 。
示例 2:
输入:s1 = "abe", s2 = "acd"
输出:false
解释:s1="abe" 的所有排列包括:"abe","aeb","bae","bea","eab" 和 "eba" ,s2="acd" 的所有排列包括:"acd","adc","cad","cda","dac" 和 "dca"。然而没有任何 s1 的排列可以打破 s2 的排列。也没有 s2 的排列能打破 s1 的排列。
示例 3:
输入:s1 = "leetcodee", s2 = "interview"
输出:true
提示:
s1.length == n
s2.length == n
1 <= n <= 10^5
所有字符串都只包含小写英文字母。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-if-a-string-can-break-another-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
这道题很有田忌赛马的味道,也用到了贪心的思想,即,如果能用最小值将对手打败,就没必要用更大的值。
所以将两个字符串分别排序后,一一对应进行比较。
用两次循环判断是否存在一个 s1 的排列可以打破 s2 的一个排列,或者是否存在一个 s2 的排列可以打破 s1 的一个排列。
时间复杂度:O(NlogN)
空间复杂度:O(N)
class Solution(object):
def checkIfCanBreak(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
s1 = sorted(s1)
s2 = sorted(s2)
flag = True
for i in range(len(s1)):
if s1[i] > s2[i]:
flag = False
break
if flag:
return True
for i in range(len(s1)):
if s1[i] < s2[i]:
return False
return True
或者利用all函数实现一行比较:
class Solution(object):
def checkIfCanBreak(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
s1 = sorted(s1)
s2 = sorted(s2)
return all(s1[i] >= s2[i] for i in range(len(s1))) or all(s2[i] >= s1[i] for i in range(len(s1)))