leetcode 647 get palindromic substring of string

leetcode 647


Problem Description:

Get the list of palindromic substrings of a string and the number of its palindromic substrings

enter:

string s

output:

A list of palindromic substrings of this string.

Ideas:

1. Use the method of extending from the middle to both sides in the substring to determine whether a string is a palindrome.

    public List<String> extendRoundCenter(String s, int start, int end) {
        List<String> res = new LinkedList<>();
        while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
            res.add(s.substring(start,end+1));
            start --;
            end ++;
        }
        return res;
    }

Important:  Every time it expands to both sides, it means that it is already a palindrome substring. A list list is used to store the palindrome substring, and finally it is returned to the function that called it for summarization.

2. When looping, use a list table to store the list of results obtained each time, and when judging whether a certain part of the string is a palindrome, pay attention to the odd and even numbers. The odd number starts to expand from itself, and the even number starts from itself. and two characters after itself begin to expand.

    public List<String> countSubstrings(String s) {
        List<String> res= new LinkedList<>();
        if (s == null || s.length() == 0) {
            return res;
        }
        int length = s.length();
        for (int i = 0; i < length; i++) {
            res.addAll(extendRoundCenter(s, i, i));
            res.addAll(extendRoundCenter(s, i, i+1));
        }
        System.out.println(res);
        System.out.println(res.size());
        return res;
    }

Important:  Note that when the calling substring is a palindrome, the former parameters (i, i) and (i, i+1) are odd numbers, centered on itself (even if there is only one single character at this time, it will be counted as palindrome), the latter is an even number and is expanded centered on two consecutive characters. Finally, returning the result list is the desired palindrome sublist.

Code:

public class lee647 {
    public static void main(String[] args) {
        lee647 lee647 = new lee647();
        lee647.countSubstrings("abcc");
    }

    public List<String> countSubstrings(String s) {
        List<String> res= new LinkedList<>();
        if (s == null || s.length() == 0) {
            return res;
        }
        int length = s.length();
        for (int i = 0; i < length; i++) {
            res.addAll(extendRoundCenter(s, i, i));
            res.addAll(extendRoundCenter(s, i, i+1));
        }
        System.out.println(res);
        System.out.println(res.size());
        return res;
    }

    public List<String> extendRoundCenter(String s, int start, int end) {
        List<String> res = new LinkedList<>();
        while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
            res.add(s.substring(start,end+1));
            start --;
            end ++;
        }
        return res;
    }
}

image

Over 96.55% of submissions

Zhang Shuangjiang

April 21, 2018 15:30:04

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324680637&siteId=291194637