这个字符串中有多少子串是某一天合法的记录序列
时间: 2024-05-04 09:18:11 浏览: 15
一个合法的记录序列指的是长度为8的仅由数字和冒号组成的字符串,且前两个字符表示小时,中间两个字符表示分钟,后两个字符表示秒钟。合法的记录序列必须满足小时在[00,23]范围内,分钟和秒钟在[00,59]范围内。
我们可以使用双重循环枚举所有可能的子串,然后判断其是否为合法的记录序列。
具体实现可以参考下面的代码:
```python
def count_legal_records(s):
count = 0
for i in range(len(s)):
for j in range(i+1, len(s)+1):
if j-i == 8:
sub = s[i:j]
if sub[0:2].isdigit() and sub[2] == ":" and sub[3:5].isdigit() and sub[5] == ":" and sub[6:].isdigit():
if 0 <= int(sub[0:2]) <= 23 and 0 <= int(sub[3:5]) <= 59 and 0 <= int(sub[6:]) <= 59:
count += 1
return count
```
该函数的时间复杂度为 $O(n^2)$,其中 $n$ 是字符串的长度。
相关问题
判断一个字符串是否有回文子串
可以通过中心扩展法来判断一个字符串是否有回文子串。具体操作是,对于字符串的每个字符作为中心点,向两边扩展判断是否对称,如果是,则表示该中心点有回文子串。具体代码实现如下:
```python
def is_palindrome(s):
for i in range(len(s)):
left, right = i, i
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
if right - left - 1 == len(s):
return True
return False
```
该函数接收一个字符串作为参数,返回一个布尔值表示该字符串是否有回文子串。
C语言中如何判断一个字符串是否包含某个子串?
在C语言中,可以使用strstr函数来判断一个字符串是否包含某个子串。
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("请输入一个字符串:");
gets(str1);
printf("请输入一个子串:");
gets(str2);
if (strstr(str1, str2) != NULL) {
printf("字符串包含子串\n");
} else {
printf("字符串不包含子串\n");
}
return 0;
}
```
上述代码中,我们使用了strstr函数来判断str1字符串是否包含str2子串。如果包含,则返回子串在字符串中的首次出现的位置的指针;如果不包含,则返回NULL。根据返回值是否为NULL,我们可以判断字符串是否包含子串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)