返回介绍

solution / 1400-1499 / 1433.Check If a String Can Break Another String / README_EN

发布于 2024-06-17 01:03:19 字数 4525 浏览 0 评论 0 收藏 0

1433. Check If a String Can Break Another String

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def checkIfCanBreak(self, s1: str, s2: str) -> bool:
    cs1 = sorted(s1)
    cs2 = sorted(s2)
    return all(a >= b for a, b in zip(cs1, cs2)) or all(
      a <= b for a, b in zip(cs1, cs2)
    )
class Solution {
  public boolean checkIfCanBreak(String s1, String s2) {
    char[] cs1 = s1.toCharArray();
    char[] cs2 = s2.toCharArray();
    Arrays.sort(cs1);
    Arrays.sort(cs2);
    return check(cs1, cs2) || check(cs2, cs1);
  }

  private boolean check(char[] cs1, char[] cs2) {
    for (int i = 0; i < cs1.length; ++i) {
      if (cs1[i] < cs2[i]) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool checkIfCanBreak(string s1, string s2) {
    sort(s1.begin(), s1.end());
    sort(s2.begin(), s2.end());
    return check(s1, s2) || check(s2, s1);
  }

  bool check(string& s1, string& s2) {
    for (int i = 0; i < s1.size(); ++i) {
      if (s1[i] < s2[i]) {
        return false;
      }
    }
    return true;
  }
};
func checkIfCanBreak(s1 string, s2 string) bool {
  cs1 := []byte(s1)
  cs2 := []byte(s2)
  sort.Slice(cs1, func(i, j int) bool { return cs1[i] < cs1[j] })
  sort.Slice(cs2, func(i, j int) bool { return cs2[i] < cs2[j] })
  check := func(cs1, cs2 []byte) bool {
    for i := range cs1 {
      if cs1[i] < cs2[i] {
        return false
      }
    }
    return true
  }
  return check(cs1, cs2) || check(cs2, cs1)
}
function checkIfCanBreak(s1: string, s2: string): boolean {
  const cs1: string[] = Array.from(s1);
  const cs2: string[] = Array.from(s2);
  cs1.sort();
  cs2.sort();
  const check = (cs1: string[], cs2: string[]) => {
    for (let i = 0; i < cs1.length; i++) {
      if (cs1[i] < cs2[i]) {
        return false;
      }
    }
    return true;
  };
  return check(cs1, cs2) || check(cs2, cs1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文