返回介绍

solution / 1900-1999 / 1930.Unique Length-3 Palindromic Subsequences / README_EN

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

1930. Unique Length-3 Palindromic Subsequences

中文文档

Description

Given a string s, return _the number of unique palindromes of length three that are a subsequence of _s.

Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.

A palindrome is a string that reads the same forwards and backwards.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

  • For example, "ace" is a subsequence of "abcde".

 

Example 1:

Input: s = "aabca"
Output: 3
Explanation: The 3 palindromic subsequences of length 3 are:
- "aba" (subsequence of "aabca")
- "aaa" (subsequence of "aabca")
- "aca" (subsequence of "aabca")

Example 2:

Input: s = "adc"
Output: 0
Explanation: There are no palindromic subsequences of length 3 in "adc".

Example 3:

Input: s = "bbcbaba"
Output: 4
Explanation: The 4 palindromic subsequences of length 3 are:
- "bbb" (subsequence of "bbcbaba")
- "bcb" (subsequence of "bbcbaba")
- "bab" (subsequence of "bbcbaba")
- "aba" (subsequence of "bbcbaba")

 

Constraints:

  • 3 <= s.length <= 105
  • s consists of only lowercase English letters.

Solutions

Solution 1

class Solution:
  def countPalindromicSubsequence(self, s: str) -> int:
    ans = 0
    for c in ascii_lowercase:
      l, r = s.find(c), s.rfind(c)
      if r - l > 1:
        ans += len(set(s[l + 1 : r]))
    return ans
class Solution {
  public int countPalindromicSubsequence(String s) {
    int ans = 0;
    for (char c = 'a'; c <= 'z'; ++c) {
      int l = s.indexOf(c), r = s.lastIndexOf(c);
      Set<Character> cs = new HashSet<>();
      for (int i = l + 1; i < r; ++i) {
        cs.add(s.charAt(i));
      }
      ans += cs.size();
    }
    return ans;
  }
}
class Solution {
public:
  int countPalindromicSubsequence(string s) {
    int ans = 0;
    for (char c = 'a'; c <= 'z'; ++c) {
      int l = s.find_first_of(c), r = s.find_last_of(c);
      unordered_set<char> cs;
      for (int i = l + 1; i < r; ++i) cs.insert(s[i]);
      ans += cs.size();
    }
    return ans;
  }
};
func countPalindromicSubsequence(s string) (ans int) {
  for c := 'a'; c <= 'z'; c++ {
    l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
    cs := map[byte]struct{}{}
    for i := l + 1; i < r; i++ {
      cs[s[i]] = struct{}{}
    }
    ans += len(cs)
  }
  return
}
public class Solution {
  public int CountPalindromicSubsequence(string s) {
    int ans = 0;
    for (char c = 'a'; c <= 'z'; ++c) {
      int l = s.IndexOf(c), r = s.LastIndexOf(c);
      HashSet<char> cs = new HashSet<char>();
      for (int i = l + 1; i < r; ++i) {
        cs.Add(s[i]);
      }
      ans += cs.Count;
    }
    return ans;
  }
}

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

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

发布评论

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