资讯专栏INFORMATION COLUMN

[LeetCode] 647. Palindromic Substrings

邹立鹏 / 2655人阅读

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:
The input string length won"t exceed 1000.

Solution

土办法,两次循环 O(n^3)

class Solution {
    public int countSubstrings(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i+1; j <= s.length(); j++) {
                String cur = s.substring(i, j);
                if (isPalindrome(cur)) count++;
            }
        }
        return count;
    }
    private boolean isPalindrome(String s) {
        if (s.length() == 1) return true;
        int i = 0, j = s.length()-1;
        while (i <= j) {
            if (s.charAt(i++) != s.charAt(j--)) return false;
        }
        return true;
    }
}
中点延展法
class Solution {
    int count = 0;
    public int countSubstrings(String s) {
        for (int i = 0; i < s.length(); i++) {
            extendFromIndex(s, i, i);
            extendFromIndex(s, i, i+1);
        }
        return count;
    }
    private void extendFromIndex(String s, int i, int j) {
        while (i >= 0 && j < s.length() && s.charAt(i--) == s.charAt(j++)) {
            count++;
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77160.html

相关文章

  • [LintCode/LeetCode] Count Binary Substrings

    Problem Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0s and 1s, and all the 0s and all the 1s in these substrings are grouped consecutively. Subs...

    BaronZhang 评论0 收藏0
  • Leetcode PHP题解--D88 696. Count Binary Substrings

    摘要:则不算,因为两个被分割开了,不是连续的。思路只记录前一组是还是,以及出现的次数。相同,则判断是否与前一个字符相同。那么此时需要抛弃前一组的所有内容。当前一组未配对字符数量达到时,说明前一组已经没有可以匹配的字符。故把当前组替换未前一组。 D88 696. Count Binary Substrings 题目链接 696. Count Binary Substrings 题目分析 给定一...

    lanffy 评论0 收藏0
  • leetcode 5 Longest Palindromic Substring Java &

    摘要:回文的意思就是反转字符串后和原字符串相等。因为这种想法没次都是两边同时扩展。所以要分目标字符串长度为奇数目标字符串为偶数两种情况。 题目详情 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.题目的意思是输入...

    JessYanCoding 评论0 收藏0
  • Leetcode 5 Longest Palindromic Substring 最长回文子串

    摘要:难度题目是说给出一个字符串求出这个字符串的最长回文的子串回文是指前后完全对称的字符串像是之类的都算是回文奇数字母的回文和偶数字母的回文中心是不一样的奇数字母比如的中心在中间字母上偶数字母比如的回文在中间两字母的中心上由此可见回文中心点实际上 Given a string s, find the longest palindromic substring in s. You may as...

    NotFound 评论0 收藏0
  • LeetCode-5 Longest Palindromic Substring

    摘要:题目解析题目是要找出最长的回文字符串,拿到题目的第一反应是遍历子串,然后一直替换最长的子字符串即可了。但是这种解法遇到极端输入状况就会超时,指定的最长长度为,遍历子串需要两次循环,判断回文需要一次循环,所以总的效率为,那么极端状况会超时。 题目 Given a string s, find the longest palindromic substring in s. You may ...

    psychola 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<