leetcode1433. Check If a String Can Break Another String
Posted seyjs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode1433. Check If a String Can Break Another String相关的知识,希望对你有一定的参考价值。
题目如下:
Given two strings:
s1
ands2
with the same size, check if some permutation of strings1
can break some permutation of strings2
or vice-versa (in other wordss2
can breaks1
).A string
x
can break stringy
(both of sizen
) ifx[i] >= y[i]
(in alphabetical order) for alli
between0
andn-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: trueConstraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
- All strings consist of lowercase English letters.
解题思路:把s1和s2按字符升序重新排列,只需要判断s1[i] >= s2[i] 或者 s1[i] <= s2[i] (i>=0 && i < len(s1) 即可。
代码如下:
class Solution(object): def checkIfCanBreak(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ l1 = sorted(list(s1)) l2 = sorted(list(s2)) res = True for i,j in zip(l1,l2): if i < j: res = False break if res : return res res = True for i,j in zip(l1,l2): if i > j: res = False break return res
以上是关于leetcode1433. Check If a String Can Break Another String的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array
LeetCode --- 1232. Check If It Is a Straight Line 解题报告
LeetCode --- 1232. Check If It Is a Straight Line 解题报告
leetcode1461. Check If a String Contains All Binary Codes of Size K
Leetcode 1961. Check If String Is a Prefix of Array
LeetCode --- 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence 解题报告