[Swift]LeetCode647. 回文子串 | Palindromic Substrings

[Swift]LeetCode647. 回文子串 | Palindromic Substrings,第1张

概述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 o

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: 3Explanation: Three palindromic strings: "a","b","c". 

Example 2:

input: "aaa"Output: 6Explanation: Six palindromic strings: "a","a","aa","aaa". 

Note:

The input string length won‘t exceed 1000.

给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。

具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。

示例 1:

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

示例 2:

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

注意:

输入的字符串长度不会超过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)10         }11         return count12     }13 14     func numpalindromes(_ s: [Character],_ i: Int,_ j: Int) -> Int {15         var count = 0,i = i,j = j16         while i >= 0,j < s.count,s[i] == s[j] {17             i -= 118             j += 119             count += 120         }21         return count22     }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,input,&result) 7             checkPalindrom(i,i + 1,&result) 8         } 9         return result10     }11     12     func checkPalindrom(_ left: Int,_ right: Int,_ input: [Character],_ result: inout Int) {13         let count = input.count14         var i = left,j = right15         while i >= 0,j < count {16             if input[i] == input[j] {17                 result += 118                 i -= 119                 j += 120             }else{21                 break22             }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 = center12             var right = center13             14             while left >= 0 && right < cnt && arr[left] == arr[right] {15                 result += 116                 left -= 117                 right += 118             }19         }20 21         for center in 1..<cnt {22             var left = center-123             var right = center24             25             while left >= 0 && right < cnt && arr[left] == arr[right] {26                 result += 127                 left -= 128                 right += 129             }30         }31         return result32     }    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+=112                 left-=113                 right+=114             }15         }16         return ans17     }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 -= 111                     right += 112                     count += 113                 }14 15             return count16         }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 = 015     for movingIndex in 1..<manipulationStr.count - 1{16         var leftIndex = movingIndex17         var rightIndex = movingIndex18         while leftIndex >= 0 && rightIndex < manipulationStr.count{19             if manipulationStr[leftIndex] != manipulationStr[rightIndex]{20                 break21             }else{22                 if manipulationStr[leftIndex] != "#"{23                     count += 124                 }25                 leftIndex -= 126                 rightIndex += 127             }28         }29     }30     return count31   }32 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode647. 回文子串 | Palindromic Substrings全部内容,希望文章能够帮你解决[Swift]LeetCode647. 回文子串 | Palindromic Substrings所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1019531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存