0
0

Leetcode 1930. Unique Length-3 Palindromic Subsequences

Posted at

アプローチ

  • hashset
  1. 同様のCharを持ってくる
  2. 同様のCharスタート時点から終了時点の間のCharを取得
  3. 上記で取得したCharをHashSetで入れる(他のCharを取得)
  4. 結果を合算する
  5. 結果を出す
class Solution {
    public int countPalindromicSubsequence(String s) {
        int result = 0;

        for (int i = 'a'; i < 'z'; i++) {
            int firstOccurence = s.indexOf(i);
            int lastOccurrence = s.lastIndexOf(i);

            if (firstOccurence != -1 && lastOccurrence != -1 && firstOccurence < lastOccurrence) {
                HashSet<Character> set = new HashSet<>();

                for (int z = firstOccurence + 1; z < lastOccurrence; z++) {

                    set.add(s.charAt(z));
                }
                result += set.size();
            }

        }

        return result;
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0