zoukankan      html  css  js  c++  java
  • [LeetCode] 1930. Unique Length-3 Palindromic Subsequences

    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.

    长度为 3 的不同回文子序列。

    给你一个字符串 s ,返回 s 中 长度为 3 的不同回文子序列 的个数。

    即便存在多种方法来构建相同的子序列,但相同的子序列只计数一次。

    回文 是正着读和反着读一样的字符串。

    子序列 是由原字符串删除其中部分字符(也可以不删除)且不改变剩余字符之间相对顺序形成的一个新字符串。

    例如,"ace" 是 "abcde" 的一个子序列。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/unique-length-3-palindromic-subsequences
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题是对回文串的考察。我提供一个比较直观的思路吧。题目问的是有多少个 unique 的,由三个字母组成的回文串的子序列。因为题目要求找的回文串只能是由三个字母组成,为了确定到底哪些回文串是合法的,我这里需要先统计一下每个字母的出现次数。只有出现次数大于等于 2 的字母,才有可能作为回文串两边的字母。

    我们需要记录 input 字符串中每个不同字母第一次和最后一次出现位置的下标,这里我用一个二维数组 int[][] indexes 记录。同时我还用另外一个一维数组 count 记录每个字母的出现次数。记录完毕之后,再次扫描 indexes 数组。对于其中的每一个字母,分如下几种情况

    • 如果当前字母出现次数 <= 1,那么他无法作为回文串两边的字母,直接就跳过了
    • 如果当前字母出现次数 > 1,他可以作为回文串两边的字母

    对于第二种情况,我们需要统计在中间到底有哪些其他的不同字母。这里我们只能再写一个 for 循环然后用 hashset 去记录。最后 hashset 的大小就是以当前字母作为回文串两边的字母能形成的合法的回文串的个数了。将这个结果累加到结果集里。

    时间O(n^2)

    空间O(n^2)

    Java实现

     1 class Solution {
     2     public int countPalindromicSubsequence(String s) {
     3         int[] count = new int[26]; // 每个字母出现的次数
     4         int[][] indexes = new int[26][2]; // 每个字母第一次和最后一次出现的index
     5         for (int i = 0; i < s.length(); i++) {
     6             if (count[s.charAt(i) - 'a'] == 0) {
     7                 indexes[s.charAt(i) - 'a'][0] = i; // 第一次出现的index
     8             }
     9             count[s.charAt(i) - 'a']++;
    10         }
    11 
    12         int[] count2 = new int[26];
    13         for (int i = s.length() - 1; i >= 0; i--) {
    14             if (count2[s.charAt(i) - 'a'] == 0) {
    15                 indexes[s.charAt(i) - 'a'][1] = i; // 最后一次出现的index
    16                 count2[s.charAt(i) - 'a'] = -1;
    17             }
    18         }
    19 
    20         int res = 0;
    21         HashSet<Character> set = new HashSet<>();
    22         for (int i = 0; i < 26; i++) {
    23             // 当前字母无法成为左右两侧的字母
    24             if (count[i] <= 1) {
    25                 continue;
    26             }
    27             int firstIndex = indexes[i][0];
    28             int lastIndex = indexes[i][1];
    29             // System.out.println(firstIndex + ", " + lastIndex + ", " + count[i]);
    30             if (lastIndex - firstIndex > 1) {
    31                 set.clear();
    32                 for (int j = firstIndex + 1; j < lastIndex; j++) {
    33                     set.add(s.charAt(j));
    34                 }
    35                 res += set.size();
    36             }
    37         }
    38         return res;
    39     }
    40 }

    二刷的时候我又尝试了一种只用两个一维数组的做法,复杂度差不多只是空间复杂度由原来的一个二维数组变成了两个一维数组。

    时间O(n^2)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int countPalindromicSubsequence(String s) {
     3         int[] count = new int[26];
     4         int[] leftMost = new int[26]; // 第一次出现的index
     5         int[] rightMost = new int[26]; // 最后一次出现的index
     6         for (int i = 0; i < s.length(); i++) {
     7             if (leftMost[s.charAt(i) - 'a'] == 0) {
     8                 leftMost[s.charAt(i) - 'a'] = i;
     9             }
    10             count[s.charAt(i) - 'a']++;
    11         }
    12         for (int i = s.length() - 1; i >= 0; i--) {
    13             if (rightMost[s.charAt(i) - 'a'] == 0) {
    14                 rightMost[s.charAt(i) - 'a'] = i;
    15             }
    16         }
    17 
    18         int res = 0;
    19         HashSet<Character> set = new HashSet<>();
    20         for (int i = 0; i < 26; i++) {
    21             // 当前字母无法成为左右两侧的字母
    22             if (count[i] <= 1) {
    23                 continue;
    24             }
    25             int firstIndex = leftMost[i];
    26             int lastIndex = rightMost[i];
    27             if (lastIndex - firstIndex > 1) {
    28                 set.clear();
    29                 for (int j = firstIndex + 1; j < lastIndex; j++) {
    30                     set.add(s.charAt(j));
    31                 }
    32                 res += set.size();
    33             }
    34         }
    35         return res;
    36     }
    37 }

    LeetCode 题目总结

  • 相关阅读:
    Hash 函数资源链接汇总
    Zookeeper 初体验之——伪分布式安装
    Zookeeper 初体验之——JAVA API 初探
    布隆过滤器(Bloom Filter)详解
    7天学会Maven(第一天——了解 Maven)
    仿中关村在线首页弹出式广告插件(jQuery版)
    介绍几款在线代码编辑器
    兼容浏览 firefox、chrome、ie 的flash(swf) 的代码!
    我的Discuz!X2 、Ucenter 1.6、ASP.NET 应用程序整合经历
    2011年最后一博:仿HAO123的邮箱登录
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15004702.html
Copyright © 2011-2022 走看看