Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created June 8, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s4553711/2f2e5e35857382b93092e9da7f769536 to your computer and use it in GitHub Desktop.
Save s4553711/2f2e5e35857382b93092e9da7f769536 to your computer and use it in GitHub Desktop.
class Solution {
public:
int countSubstrings(string s) {
int ans = 0;
for (int i = 0; i < s.length();i++) {
ans += count(s, i, i);
ans += count(s, i, i+1);
}
return ans;
}
int count(string& s, int l, int r) {
int ans = 0;
while(l >= 0 && r < s.length() && s[l--] == s[r++]) ++ans;
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment