推 Meaverzt: 大師 01/04 14:30
※ 引述《dont (dont)》之銘言:
: 1930. Unique Length-3 Palindromic Subsequences
思路差不多
找到最左和最右的字母然後檢查中間有幾種字母
本來以為這種解算是滿暴力的但是跑出來還滿快的
java code:
--------------------------------------------------
class Solution {
public int countPalindromicSubsequence(String s) {
int res = 0;
for (char c = 'a'; c <= 'z'; c = (char) (c + 1)) {
boolean[] words = new boolean[26];
int l = s.indexOf(c) + 1;
int r = s.lastIndexOf(c) - 1;
while (l <= r) {
words[s.charAt(l++) - 'a'] = true;
}
for (boolean word : words) {
if (word) res++;
}
}
return res;
}
}
--------------------------------------------------
--
https://i.imgur.com/5xKbxoh.jpeg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.101.161 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1735972105.A.7C6.html