Skip to content

Commit

Permalink
1930. Unique Length-3 Palindromic Subsequences
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeed205 committed Nov 14, 2023
1 parent 6731ac8 commit 8c1e11c
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.HashSet;

class Solution {
public int countPalindromicSubsequence(String inputString) {
int[] minExist = new int[26];
int[] maxExist = new int[26];
for (int i = 0; i < 26; i++) {
minExist[i] = Integer.MAX_VALUE;
maxExist[i] = Integer.MIN_VALUE;
}

for (int i = 0; i < inputString.length(); i++) {
int charIndex = inputString.charAt(i) - 'a';
minExist[charIndex] = Math.min(minExist[charIndex], i);
maxExist[charIndex] = Math.max(maxExist[charIndex], i);
}

int uniqueCount = 0;

for (int charIndex = 0; charIndex < 26; charIndex++) {
if (minExist[charIndex] == Integer.MAX_VALUE || maxExist[charIndex] == Integer.MIN_VALUE) {
continue;
}

HashSet<Character> uniqueCharsBetween = new HashSet<>();

for (int j = minExist[charIndex] + 1; j < maxExist[charIndex]; j++) {
uniqueCharsBetween.add(inputString.charAt(j));
}

uniqueCount += uniqueCharsBetween.size();
}

return uniqueCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {string} s
* @return {number}
*/
var countPalindromicSubsequence = function (s) {
const charToIndex = (char) => char.charCodeAt(0) - "a".charCodeAt(0);

const sBytes = Array.from(s, charToIndex);
const left = new Array(26).fill(Number.MAX_SAFE_INTEGER);
const right = new Array(26).fill(0);
let ret = 0;

for (let i = 0; i < s.length; i++) {
const j = sBytes[i];
left[j] = Math.min(left[j], i);
right[j] = i;
}

const used = new Array(26).fill(false);
for (let i = 0; i < 26; i++) {
ret += countUniqueBetween(sBytes, left[i] + 1, right[i], used);
}

return ret;
};

function countUniqueBetween(s, start, end, used) {
let count = 0;
for (let j = start; j < end; j++) {
const idx = s[j];
if (!used[idx]) {
used[idx] = true;
count += 1;
}
}
used.fill(false);
return count;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
def count_unique_between(start, end, used):
count = 0
for j in range(start, end):
idx = ord(s[j]) - ord('a')
if not used[idx]:
used[idx] = True
count += 1
for idx in range(26):
used[idx] = False # Resetting the values to False
return count

s_bytes = [ord(char) - ord('a') for char in s]
left = [float('inf')] * 26
right = [0] * 26
ret = 0

for i, char in enumerate(s_bytes):
left[char] = min(left[char], i)
right[char] = i

used = [False] * 26
for i in range(26):
if left[i] != float('inf'): # Check if left[i] is a valid index
ret += count_unique_between(left[i] + 1, right[i], used)

return ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
impl Solution {
pub fn count_palindromic_subsequence(s: String) -> i32 {
let s = s.into_bytes();
let mut ret = 0;

let mut left = [usize::MAX; 26];
let mut right = [0; 26];

for (i, &b) in s.iter().enumerate() {
let j = (b - b'a') as usize;
left[j] = left[j].min(i);
right[j] = i;
}

let mut used = [false; 26];
for i in 0..26 {
ret += count_unique_between(&s, left[i] + 1, right[i], &mut used);
}

ret
}
}

fn count_unique_between(s: &[u8], start: usize, end: usize, used: &mut [bool; 26]) -> i32 {
let mut count = 0;
for j in start..end {
let idx = (s[j] - b'a') as usize;
if !used[idx] {
used[idx] = true;
count += 1;
}
}
used.fill(false);
count
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function countPalindromicSubsequence(s: string): number {
const charToIndex = (char: string): number =>
char.charCodeAt(0) - "a".charCodeAt(0);

const sBytes: number[] = Array.from(s, charToIndex);
const left: number[] = new Array(26).fill(Number.MAX_SAFE_INTEGER);
const right: number[] = new Array(26).fill(0);
let ret = 0;

for (let i = 0; i < s.length; i++) {
const j = sBytes[i];
left[j] = Math.min(left[j], i);
right[j] = i;
}

const used: boolean[] = new Array(26).fill(false);
for (let i = 0; i < 26; i++) {
ret += countUniqueBetween(sBytes, left[i] + 1, right[i], used);
}

return ret;
}

function countUniqueBetween(
s: number[],
start: number,
end: number,
used: boolean[]
): number {
let count = 0;
for (let j = start; j < end; j++) {
const idx = s[j];
if (!used[idx]) {
used[idx] = true;
count += 1;
}
}
used.fill(false);
return count;
}
47 changes: 47 additions & 0 deletions Medium/1930. Unique Length-3 Palindromic Subsequences/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## [1930. Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/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:

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

#### Example 2:

<pre>
<strong>Input:</strong> s = "adc"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no palindromic subsequences of length 3 in "adc".
</pre>

#### Example 3:

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

#### Constraints:

- <code>3 <= s.length <= 10<sup>5</sup></code>
- `s` consists of only lowercase English letters.

0 comments on commit 8c1e11c

Please sign in to comment.