返回介绍

solution / 0500-0599 / 0522.Longest Uncommon Subsequence II / README_EN

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

522. Longest Uncommon Subsequence II

中文文档

Description

Given an array of strings strs, return _the length of the longest uncommon subsequence between them_. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.

A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.

  • For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).

 

Example 1:

Input: strs = ["aba","cdc","eae"]
Output: 3

Example 2:

Input: strs = ["aaa","aaa","aa"]
Output: -1

 

Constraints:

  • 2 <= strs.length <= 50
  • 1 <= strs[i].length <= 10
  • strs[i] consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def findLUSlength(self, strs: List[str]) -> int:
    def check(a, b):
      i = j = 0
      while i < len(a) and j < len(b):
        if a[i] == b[j]:
          j += 1
        i += 1
      return j == len(b)

    n = len(strs)
    ans = -1

    for i in range(n):
      j = 0
      while j < n:
        if i == j or not check(strs[j], strs[i]):
          j += 1
        else:
          break
      if j == n:
        ans = max(ans, len(strs[i]))
    return ans
class Solution {
  public int findLUSlength(String[] strs) {
    int ans = -1;
    for (int i = 0, j = 0, n = strs.length; i < n; ++i) {
      for (j = 0; j < n; ++j) {
        if (i == j) {
          continue;
        }
        if (check(strs[j], strs[i])) {
          break;
        }
      }
      if (j == n) {
        ans = Math.max(ans, strs[i].length());
      }
    }
    return ans;
  }

  private boolean check(String a, String b) {
    int j = 0;
    for (int i = 0; i < a.length() && j < b.length(); ++i) {
      if (a.charAt(i) == b.charAt(j)) {
        ++j;
      }
    }
    return j == b.length();
  }
}
class Solution {
public:
  int findLUSlength(vector<string>& strs) {
    int ans = -1;
    for (int i = 0, j = 0, n = strs.size(); i < n; ++i) {
      for (j = 0; j < n; ++j) {
        if (i == j) continue;
        if (check(strs[j], strs[i])) break;
      }
      if (j == n) ans = max(ans, (int) strs[i].size());
    }
    return ans;
  }

  bool check(string a, string b) {
    int j = 0;
    for (int i = 0; i < a.size() && j < b.size(); ++i)
      if (a[i] == b[j]) ++j;
    return j == b.size();
  }
};
func findLUSlength(strs []string) int {
  check := func(a, b string) bool {
    j := 0
    for i := 0; i < len(a) && j < len(b); i++ {
      if a[i] == b[j] {
        j++
      }
    }
    return j == len(b)
  }

  ans := -1
  for i, j, n := 0, 0, len(strs); i < n; i++ {
    for j = 0; j < n; j++ {
      if i == j {
        continue
      }
      if check(strs[j], strs[i]) {
        break
      }
    }
    if j == n && ans < len(strs[i]) {
      ans = len(strs[i])
    }
  }
  return ans
}

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

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

发布评论

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