[Swift]LeetCode647. 回文子串 | 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:

输入: "abc"
输出: 3
解释: 三个回文子串: "a", "b", "c".

示例 2:

输入: "aaa"
输出: 6
说明: 6个回文子串: "a", "a", "a", "aa", "aa", "aaa".

注意:

  1. 输入的字符串长度不会超过1000。


16ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3         var count = 0
 4         var s = Array(s)
 5         for i in 0..<s.count {
 6             if i + 1 < s.count, s[i] == s[i + 1] {
 7                 count += numPalindromes(s, i, i + 1)
 8             }
 9             count += numPalindromes(s, i, i)
10         }
11         return count
12     }
13
14     func numPalindromes(_ s: [Character], _ i: Int, _ j: Int) -> Int {
15         var count = 0, i = i, j = j
16         while i >= 0, j < s.count, s[i] == s[j] {
17             i -= 1
18             j += 1
19             count += 1
20         }
21         return count
22     }
23 }


20ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3         var result = 0
 4         let input = Array(s)
 5         for i in 0..<input.count {
 6             checkPalindrom(i,i,input,&result)
 7             checkPalindrom(i,i + 1,input,&result)
 8         }
 9         return result
10     }
11
12     func checkPalindrom(_ left: Int, _ right: Int, _ input: [Character], _ result: inout Int) {
13         let count = input.count
14         var i = left, j = right
15         while i >= 0, j < count {
16             if input[i] == input[j] {
17                 result += 1
18                 i -= 1
19                 j += 1
20             }else{
21                 break
22             }
23         }
24     }
25 }


28ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3         let arr = Array(s)
 4         let cnt = s.count
 5         if cnt == 0 {
 6             return 0
 7         }
 8         var result = 0
 9
10         for center in 0..<cnt {
11             var left = center
12             var right = center
13
14             while left >= 0 && right < cnt && arr[left] == arr[right] {
15                 result += 1
16                 left -= 1
17                 right += 1
18             }
19         }
20
21         for center in 1..<cnt {
22             var left = center-1
23             var right = center
24
25             while left >= 0 && right < cnt && arr[left] == arr[right] {
26                 result += 1
27                 left -= 1
28                 right += 1
29             }
30         }
31         return result
32     }
33 }


32ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3         let s = Array(s)
 4         var n = s.count, ans = 0
 5         for center in 0..<2*n {
 6             var left = center / 2
 7             var right = left + center % 2
 8             while (left >= 0 && right < n &&
 9                    s[s.index(s.startIndex,offsetBy:left)] ==
10                    s[s.index(s.startIndex,offsetBy:right)]) {
11                 ans+=1
12                 left-=1
13                 right+=1
14             }
15         }
16         return ans
17     }
18 }


40ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3         let chars = Array(s)
 4         func extend(_ left: Int, _ right: Int) -> Int {
 5             var (left, right) = (left, right)
 6             var count = 0
 7             while left >= 0
 8                 && right < chars.count
 9                 && chars[left] == chars[right] {
10                     left -= 1
11                     right += 1
12                     count += 1
13                 }
14
15             return count
16         }
17
18         return chars.indices.map {
19             extend($0, $0) + extend($0, $0 + 1)
20         }
21         .reduce(0, +)
22     }
23 }


56ms

 1 class Solution {
 2     func countSubstrings(_ s: String) -> Int {
 3     guard s.count > 1 else{
 4         return 1
 5     }
 6
 7     var manipulationStr : [Character] = []
 8     for char in Array(s){
 9         manipulationStr.append("#")
10         manipulationStr.append(char)
11     }
12     manipulationStr.append("#")
13
14     var count : Int = 0
15     for movingIndex in 1..<manipulationStr.count - 1{
16         var leftIndex = movingIndex
17         var rightIndex = movingIndex
18         while leftIndex >= 0 && rightIndex < manipulationStr.count{
19             if manipulationStr[leftIndex] != manipulationStr[rightIndex]{
20                 break
21             }else{
22                 if manipulationStr[leftIndex] != "#"{
23                     count += 1
24                 }
25                 leftIndex -= 1
26                 rightIndex += 1
27             }
28         }
29     }
30     return count
31   }
32 }

原文地址:https://www.cnblogs.com/strengthen/p/10482491.html

时间: 2024-08-16 16:34:35

[Swift]LeetCode647. 回文子串 | Palindromic Substrings的相关文章

LeetCode 647. 回文子串(Palindromic Substrings)

647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. LeetCode647. Palindromic Substrings中等 示例 1: 输入: "abc" 输出: 3 解释: 3 个回文子串: "a", "b", "c". 示例 2: 输入: &

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" Ou

Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "abc" 输出: 3 解释: 三个回文子串: "a", "b", "c". 示例 2: 输入: "aaa" 输出: 6 说明: 6个回文子串: "a", "a", "a", &quo

[Leetcode] Longest palindromic substring 最长回文子串

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

[C++]LeetCode: 99 Longest Palindromic Substring (最长回文子串)

题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

[译]最长回文子串(Longest Palindromic Substring) Part II

[译+改]最长回文子串(Longest Palindromic Substring) Part II 问题:给定字符串S,求S中的最长回文子串. 在上一篇,我们给出了4种算法,其中包括一个O(N2)时间O(1)空间的算法(中心检测法),已经很不错了.本篇将讨论一个O(N)时间O(N)空间的算法,即著名的Manacher算法,并详细说明其时间复杂度为何是O(N). 提示 +BIT祝威+悄悄在此留下版了个权的信息说: 先想想有什么办法能改进中心检测法. 考虑一下最坏的情况.★ 最坏的情况就是各个回文

leetcode 5 :Longest Palindromic Substring 找出最长回文子串

题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 翻译: 找出字符串s中最长的回文子串,字符串s的最长是1000,假设存在唯一的最长回文子串 法一:直接暴力破解 O(N3)的时间复杂度,运行超

【LeetCode】5. Longest Palindromic Substring 最大回文子串

题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:用Manacher算法得到最大回文子串的长度,得到带#的最大回文子串,用split剔除#后转化成String形式输出即可. public

转载:LeetCode:5Longest Palindromic Substring 最长回文子串

本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 求字符串的最长回文子串 算法1:暴