码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】1433. Check If a String Can Break Another String

时间:2020-06-03 23:41:15      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:题目   rom   bsp   let   NPU   bool   class   word   int   

题目如下:

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).

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.

解题思路:把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

 

【leetcode】1433. Check If a String Can Break Another String

标签:题目   rom   bsp   let   NPU   bool   class   word   int   

原文地址:https://www.cnblogs.com/seyjs/p/13040537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!