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 }

 

转载于:https://www.cnblogs.com/liangmou/p/8384493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值