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

Commit8f71f5a

Browse files
authored
Merge pull requestneetcode-gh#2199 from seinlin/1930
Add 1930-unique-length-3-palindromic-subsequences.c
2 parents4ea18b1 +5219495 commit8f71f5a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
intcountPalindromicSubsequence(char*s){
2+
intn=strlen(s);
3+
inti,j;
4+
intc,answer;
5+
intfirst[26]= {[0 ...25]=-1};
6+
intlast[26]= {[0 ...25]=-1};
7+
8+
// Use two arrays to record the first and last
9+
// location of each character.
10+
for (i=0;i<n;i++) {
11+
c=s[i]-'a';
12+
if (first[c]==-1) {
13+
first[c]=i;
14+
}else {
15+
last[c]=i;
16+
}
17+
}
18+
19+
// When a character appears twice, count all the
20+
// unique character between them.
21+
answer=0;
22+
for (i=0;i<26;i++) {
23+
if (last[i]!=-1) {
24+
intm[26]= {[0 ...25]=-1};
25+
for (j=first[i]+1;j<last[i];j++) {
26+
intc=s[j]-'a';
27+
if (m[c]==-1) {
28+
m[c]=j;
29+
answer++;
30+
}
31+
}
32+
}
33+
}
34+
35+
returnanswer;
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp