CareerCup Facebook Total number of substring palindrome Leetcode 647

265 篇文章 1 订阅

Write a function for retrieving the total number of substring palindromes. 
For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba 
So the result is 6. 

Updated at 11/11/2013: 

 

After the interview I got know that the O(n^3) solution is not enough to go to the next round. It would have been better to know before starting implementing the solution unnecessarily ...

 

------------------------------------------------------------------------

Similar to leetcode Longest Palindromic Substring Part II in my blog, the code is like:

 

#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include <vector>
using namespace std;
string preprocess(string s) {
  string res = "^#";
  for (int i = 0; i < s.length(); ++i) {
    res += s[i];
    res += '#';
  }
  res += '$';
  return res;
}
int getPalindromeNum(string s) {
  string str = preprocess(s);
  int i, j, len = str.length(), C = 0, R = 0, res = 0, ii;

  vector<int> T(len + 1, 0), P(len + 1, 0);

  for (i = 1; i < len; ++i) {
    ii = 2*C - i;
    P[i] = (R - i) > 0 ? min(P[ii], R-i) : 0;
    //bug1: P[i] = (R - i) > 0 ? P[i] : 0
    while (str[i + P[i] + 1] == str[i - P[i] - 1])
      ++P[i];
    res += (P[i] + 1) / 2;
    //bug2: res += P[i]; 
    if (i + P[i] > R) {
      C = i;
      R = i + P[i];
    }
  }
  return res;
}
int main() {
  //string s = "abcba";
  string s = "aaaaa";
  
  int res = getPalindromeNum(s);
  return 0;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Leetcode collects this problem:

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.

 

Accepted

147,409

Submissions

249,379

-----------------------------------------------------------

Python 

class Solution:
    def pre_process(self, s):
        res = '^#'
        for ch in s:
            res += ch + '#'
        res += '$'
        return res

    def countSubstrings(self, s):
        s1 = self.pre_process(s)
        l, res, c, r = len(s1), 0, 0, 0
        p = [0 for i in range(l)]

        for i in range(1, l-1): #bug1: for i in range(1, l-1)
            ii = c * 2 - i
            # bug2: p[i] = 0 if ii < 0 else min(p[ii], r)
            p[i] = 0 if ii < 0 else min(p[ii], r-i)
            while (s1[i + p[i] + 1] == s1[i - p[i] - 1]):
                p[i] += 1
            if (i + p[i] > r):
                r = i + p[i]
                c = i
            res += (p[i] + 1) >> 1
        return res

s = Solution()
print(s.countSubstrings("aaa"))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值