-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0647-palindromic-substrings" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# | ||
|
||
[问题描述](https://leetcode.com/problems/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
def countSubstrings(s: str) -> int: | ||
# 回文字符串的一个特性是中间对齐, 我们可以遍历所有字符, 以它为中间, 尝试 | ||
# 向左右两侧扩展, 检查它们是否相等, 如果相等, 就说明这个 substring 是回文的. | ||
|
||
palindrome_count = 0 | ||
|
||
def twoSideEquals(s: str, left: int, right: int) -> bool: | ||
# 先检查两个索引值是否有效, 再检查它们指向的字符是否相等 | ||
if left >= 0 and right < len(s) and s[left] == s[right]: | ||
return True | ||
else: | ||
return False | ||
|
||
# middle 是 substring 的中间节点 | ||
for middle in range(len(s)): | ||
# 如果 substring 中包含奇数个字符, 那就以 (middle) 作为中间对齐节点 | ||
left = middle | ||
right = middle | ||
while twoSideEquals(s, left, right): | ||
palindrome_count += 1 | ||
left -= 1 | ||
right += 1 | ||
|
||
# 如果 substring 中包含偶数个字符, 那就以 (middle, middle+1) 作为中间对齐节点 | ||
left = middle | ||
right = middle + 1 | ||
while twoSideEquals(s, left, right): | ||
palindrome_count += 1 | ||
left -= 1 | ||
right += 1 | ||
|
||
# 返回结果 | ||
return palindrome_count | ||
|
||
def main(): | ||
s1 = "abc" | ||
assert(countSubstrings(s1) == 3) | ||
|
||
s2 = "aaa" | ||
assert(countSubstrings(s2) == 6) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
pub fn solution1() { | ||
todo!(); | ||
} | ||
|
||
pub type SolutionFn = fn(); | ||
|
||
fn check_solution(_func: SolutionFn) { | ||
todo!(); | ||
} | ||
|
||
fn main() { | ||
check_solution(solution1); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, solution1}; | ||
|
||
#[test] | ||
fn test_solution1() { | ||
check_solution(solution1); | ||
} | ||
} |