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.
"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 数组。对于其中的每一个字母,分如下几种情况
对于第二种情况,我们需要统计在中间到底有哪些其他的不同字母。这里我们只能再写一个 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 }
LeetCode 题目总结