当前位置:网站首页>2021-12-22: palindrome substring. Give you a string s, please count and return
2021-12-22: palindrome substring. Give you a string s, please count and return
2022-06-23 21:33:00 【Fuda scaffold constructor's daily question】
2021-12-22: Palindrome string .
Give you a string s , Please count and return this string Palindrome string Number of .
Palindrome string Is reading the same string as reading it backwards .
Substring Is a sequence of consecutive characters in a string .
A substring with different start or end positions , Even if it's made up of the same characters , It's also seen as a different substring .
Example 1:
Input :s = "abc",
Output :3,
explain : Three palindrome strings : "a", "b", "c".
Example 2:
Input :s = "aaa",
Output :6,
explain :6 Palindrome substrings : "a", "a", "a", "aa", "aa", "aaa".
Tips :
1 <= s.length <= 1000,
s It's made up of lowercase letters .
Power button 647.
answer 2021-12-22:
Horse drawn car algorithm . Count each center and then sum .
Time complexity :O(n).
Spatial complexity :O(n).
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
s := "moonfdd"
ret := countSubstrings(s)
fmt.Println(ret)
}
func countSubstrings(s string) int {
if len(s) == 0 {
return 0
}
dp := getManacherDP(s)
ans := 0
for i := 0; i < len(dp); i++ {
ans += dp[i] >> 1
}
return ans
}
func getManacherDP(s string) []int {
str := manacherString(s)
pArr := make([]int, len(str))
C := -1
R := -1
for i := 0; i < len(str); i++ {
if R > i {
pArr[i] = getMin(pArr[2*C-i], R-i)
} else {
pArr[i] = 1
}
for i+pArr[i] < len(str) && i-pArr[i] > -1 {
if str[i+pArr[i]] == str[i-pArr[i]] {
pArr[i]++
} else {
break
}
}
if i+pArr[i] > R {
R = i + pArr[i]
C = i
}
}
return pArr
}
func manacherString(str string) []byte {
charArr := []byte(str)
res := make([]byte, len(str)*2+1)
index := 0
for i := 0; i != len(res); i++ {
if i&1 == 0 {
res[i] = '#'
} else {
res[i] = charArr[index]
index++
}
}
return res
}
func getMin(a, b int) int {
if a < b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- [typescript] some summaries in actual combat
- Polling and connection
- It's very interesting. Make an app to decorate the Christmas hat on Christmas!
- Is it safe to open an account for flush stock?
- Who do you want to open a stock account? Is it safe to open an account online?
- Disaster recovery series (VII) -- hybrid cloud public network export disaster recovery construction
- 【etcd】etcd使用与集群搭建
- 发现一个大佬云集的宝藏硕博社群!
- How to reduce snapshots
- Global and Chinese markets of natural starch 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Application of JDBC in performance test

Lightweight, dynamic and smooth listening, hero earphone hands-on experience, can really create

How does PMO select and train project managers?
![Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text](/img/b1/71cc36c45102bdb9c06e099eb42267.jpg)
Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text

蓝牙芯片|瑞萨和TI推出新蓝牙芯片,试试伦茨科技ST17H65蓝牙BLE5.2芯片

Facing the problem of lock waiting, how to realize the second level positioning and analysis of data warehouse

大一女生废话编程爆火!懂不懂编程的看完都拴Q了

How to view the role of PMO in agile organizations?

I am 30 years old, no longer young, and have nothing

What are the main dimensions of PMO performance appraisal?
随机推荐
[hot sales at the beginning of the year] | the first special offer of popular cloud products is second to none, and the first year of 1-core 2G cloud server is 38 yuan!
RI Gai series: push of STD container_ Why is back slower than []
Global and Chinese market of American football catch gloves 2022-2028: Research Report on technology, participants, trends, market size and share
Using clion to realize STM32F103 lighting LED
Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text
Cobalt Strike Spawn & Tunnel
Authentication can be as simple as this - use the API gateway to protect your API security
Game security - call analysis - write code
Summary of multiple methods for obtaining the last element of JS array
A detailed discussion on the use guide of network Swiss Army knife nmap
Share a super Mary source code
Full text search of MySQL
Gradle asked seven times. You should know that~
What is a database index? Xinhua dictionary to help you
股票开户要找谁?网上开户安全么?
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
Troubleshooting of black screen after easynvr is cascaded to the upper platform and played for one minute
How to Net project migration to NET Core
Real time crawler launches a number of special new products
JS to get the screen size, current web page and browser window