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

Commitad095f8

Browse files
authored
Merge pull requestneetcode-gh#1023 from julienChemillier/patch-4
Add 647 in language c
2 parentsc2fced0 +31f812a commitad095f8

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎c/647-Palindromic-Substrings.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Given a string s, return the number of palindromic substrings in it.
3+
A string is a palindrome when it reads the same backward as forward.
4+
A substring is a contiguous sequence of characters within the string.
5+
Time: O(n^2)
6+
Space: O(1)
7+
*/
8+
9+
intcountSubstrings(char*s){
10+
intcpt=0;
11+
intx,y;
12+
13+
for (inti=0;s[i]!='\0';i++){
14+
cpt++;// s[i] is a palindrom
15+
x=i-1;
16+
y=i+1;
17+
while (x>=0&&s[y]!='\0'&&s[x]==s[y]){// Odd length palindromic substring
18+
cpt++;
19+
x--;
20+
y++;
21+
}
22+
x=i;
23+
y=i+1;
24+
while (x>=0&&s[y]!='\0'&&s[x]==s[y]){// Even length palindromic substring
25+
cpt++;
26+
x--;
27+
y++;
28+
}
29+
}
30+
returncpt;
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp