zoukankan      html  css  js  c++  java
  • 647. Palindromic Substrings

    package LeetCode_647
    
    /**
     * 647. Palindromic Substrings
     * https://leetcode.com/problems/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.
     * */
    class Solution {
        /*
        * solution: check the palindrome of odd length palindromic substring and even length palindromic substring,
        * for example: aabaa, checking:
         loop 1. center on a and aa, scan to left and right,
         2. center on a and ab, scan to left and right,
         3. center on b and ba, scan to left and right,
    
        * Time complexity:O(n^2), Space complexity:O(1)
        * */
    
        private var count = 0
    
        fun countSubstrings(s: String): Int {
            if (s == null || s.isEmpty()) {
                return 0
            }
            for (i in s.indices) {
                //check odd length
                checkPalindrome(s, i, i)
                //check even length
                checkPalindrome(s, i, i + 1)
            }
            return count
        }
    
        private fun checkPalindrome(string: String, i_: Int, j_: Int) {
            var i = i_
            var j = j_
            while (i >= 0 && j < string.length && string[i] == string[j]) {
                //if substring is palindrome
                count++
                //to trace string in left direction
                i--
                //to trace string in right direction
                j++
            }
        }
    }
  • 相关阅读:
    jvm 优化
    SqlServer体系结构
    sqlserver2012 在视图中建索引
    win10 桌面设置为远程桌面
    ORACLE 查询某表中的某个字段的类型,是否为空,是否有默认值等
    activemq读取剩余消息队列中消息的数量
    Oracl 一条sql语句 批量添加、修改数据
    ClickOnce一项Winform部署
    C#语言中的修饰符
    关于MySQL集群的一些看法
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13939406.html
Copyright © 2011-2022 走看看