Leetcode 647: Palindromic Substrings

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 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"
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 public class Solution {
 2     public int CountSubstrings(string s) {
 3         if (s == null || s.Length == 0) return 0;
 4         
 5         var dp = new bool[s.Length, s.Length];
 6         
 7         int count = 0;
 8         
 9         for (int i = s.Length - 1; i >= 0; i--)
10         {
11             for (int j = i; j < s.Length; j++)
12             {
13                 if ((s[i] == s[j]) && (j - i <= 2  || dp[i + 1, j - 1]))
14                 {
15                     dp[i, j] = true;
16                     count++;
17                 }
18             }
19         }
20         
21         return count;
22     }
23 }
24 
25 public class Solution1 {
26     public int CountSubstrings(string s) {
27         if (s == null || s.Length == 0) return 0;
28         
29         int count = 0;
30         var dict = new Dictionary<Tuple<int, int>, bool>();
31         
32         for (int i = s.Length - 1; i >= 0; i--)
33         {
34             for (int j = i; j < s.Length; j++)
35             {
36                 if (IsPalindrome(s, i, j, dict)) count++;    
37             }
38         }
39         
40         return count;
41     }
42     
43     private bool IsPalindrome(string s, int i, int j, Dictionary<Tuple<int, int>, bool> dict)
44     {
45         var key = new Tuple<int, int>(i, j);
46         
47         if (i == j)
48         {
49             dict[key] = true;
50             return true;
51         }
52         
53         if (i + 1 == j)
54         {
55             if (s[i] == s[j])
56             {
57                 dict[key] = true;
58                 return true;
59             }
60             else
61             {
62                 dict[key] = false;
63                 return false;
64             }
65         }
66         
67         var key1 = new Tuple<int, int>(i + 1, j - 1);
68         
69         // since we have aleady processed them before, we don‘t need to process again
70         if (s[i] == s[j] && dict.ContainsKey(key1) && dict[key1])
71         {
72             dict[key] = true;
73             return true;
74         }
75         else
76         {
77             dict[key] = false;
78             return false;
79         }
80     }
81 }

 

以上是关于Leetcode 647: Palindromic Substrings的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 647. Palindromic Substrings

LeetCode 647. 回文子串(Palindromic Substrings)

Leetcode 647. Palindromic Substrings

[动态规划] leetcode 647 Palindromic Substrings

[Leetcode]647.Palindromic Substrings

[Leetcode]647.Palindromic Substrings