Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10fbabe

Browse files
committed
Create: 1930-unique-length-3-palindromic
1 parent49fff18 commit10fbabe

4 files changed

+75
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*@param {string} s
3+
*@return {number}
4+
*/
5+
varcountPalindromicSubsequence=function(s){
6+
letcount=0;
7+
letchars=newSet(s);
8+
for(constcharofchars){
9+
letfirst=s.indexOf(char),last=s.lastIndexOf(char);
10+
count+=newSet(s.slice(first+1,last)).size;
11+
}
12+
returncount;
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
classSolution:
2+
defcountPalindromicSubsequence(self,s:str)->int:
3+
count=0
4+
chars=set(s)
5+
forcharinchars:
6+
first,last=s.find(char),s.rfind(char)
7+
count+=len(set(s[first+1:last]))
8+
returncount
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::iter::FromIterator;
2+
3+
implSolution{
4+
pubfncount_palindromic_subsequence(s:String) ->i32{
5+
letmut result =0;
6+
letmut ranges:[(i32,i32);26] =[(-1, -1);26];
7+
8+
for(i, c)in s.chars().enumerate(){
9+
let ix =Solution::char_to_index(c)asusize;
10+
if ranges[ix].0 == -1{
11+
ranges[ix].0 = iasi32;
12+
}
13+
if iasi32 > ranges[ix].1{
14+
ranges[ix].1 = iasi32;
15+
}
16+
}
17+
18+
for rangein ranges{
19+
if range.1 > range.0{
20+
letmut set:u32 =0;
21+
for cin s[range.0asusize +1..range.1asusize].chars(){
22+
set |=1 <<Solution::char_to_index(c);
23+
}
24+
result += set.count_ones()asi32;
25+
}
26+
}
27+
28+
result
29+
}
30+
31+
pubfnchar_to_index(c:char) ->u32{
32+
casu32 -'a'asu32
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
functioncountPalindromicSubsequence(s:string):number{
2+
constalphabets='abcdefghijklmnopqrstuvwxyz';
3+
4+
constN=alphabets.length;
5+
letcount=0;
6+
7+
for(leti=0;i<N;i+=1){
8+
constch=alphabets[i];
9+
constleft=s.indexOf(ch);
10+
constright=s.lastIndexOf(ch);
11+
if(left<right){
12+
for(constalphaofalphabets){
13+
constmid=s.indexOf(alpha,left+1);
14+
if(mid!==-1&&mid<right)count+=1;
15+
}
16+
}
17+
}
18+
19+
returncount;
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp