647. Palindromic Substrings(LeetCode)

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:

Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

Note:

  1. The input string length won‘t exceed 1000.

     1 class Solution {
     2 public:
     3     int countSubstrings(string s) {
     4        string str1="";
     5         string str2 = "";
     6         int len = s.size();
     7         int count = 0;
     8         for (int i = 0; i < len; i++)
     9         {
    10             for (int j = 1; j+i <= len; j++)
    11             {
    12                 str1 = s.substr(i, j);
    13                 //cout << i << " " << j << " " << str1 << endl;
    14                 str2 = str1;
    15                 reverse(str1.begin(), str1.end());
    16                 if (str2 == str1)
    17                     count++;
    18                 str2 = "";
    19                 str1 = "";
    20             }
    21         }
    22         return count;
    23     }
    24 };
时间: 2024-12-08 01:14:01

647. Palindromic Substrings(LeetCode)的相关文章

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

Substrings(hdu1238)字符串匹配

Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7205 Accepted Submission(s): 3255 Problem Description You are given a number of case-sensitive strings of alphabetic characters, find the

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"

不邻接植花(leetcode)

有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或者离开. 你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同. 以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类.花的种类用  1, 2, 3, 4 表示.保证存在答案. 示例 1: 输入:N = 3,

647. Palindromic Substrings 回文子串的数量

Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc" Ou

647. Palindromic Substrings

问题描述: Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc&qu

领扣(LeetCode)二维区域和检索 个人题解

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8. 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegi

Distinct Substrings (spoj694)(后缀数组)

核心:每一个后缀的每一个前缀都是一个子串 相邻后缀的lcp的值是就是重复子串的个数 用所有的子串减去sigma(height)就行了 1 #include"bits/stdc++.h" 2 using namespace std; 3 #define clr(x) memset(x,0,sizeof(x)) 4 5 int T; 6 char a[2000]; 7 int n,m; 8 int x[2000],y[2000],c[2000]; 9 int sa[2000]; 10 in