完成函数find_rep_str,该函数接受字符串s,并查找所有重复的字符,这些字符是s的子字符串。子字符串应添加到全局集result中。
时间: 2023-05-20 11:03:58 浏览: 90
以下是find_rep_str函数的完整代码:
```lua
local result = {}
function find_rep_str(s)
local len = #s
for i = 1, len do
for j = i + 1, len do
local sub_str = string.sub(s, i, j)
if string.find(s, sub_str, j + 1) then
table.insert(result, sub_str)
end
end
end
end
```
该函数使用两个嵌套的循环来遍历字符串s中的所有可能的子字符串。对于每个子字符串,它检查是否存在另一个相同的子字符串,如果存在,则将其添加到全局集result中。最后,该函数返回result集合,其中包含所有重复的子字符串。
请注意,该函数只查找长度大于1的子字符串。如果您想要查找长度为1的重复字符,请将内部循环的起始值更改为1。
相关问题
完成函数find_rep_str,该函数接受字符串s,并查找所有重复的字符,子字符串应添加到全局集result中。
以下是find_rep_str函数的完整代码:
```lua
local result = {}
function find_rep_str(s)
local char_set = {}
for i = 1, #s do
local char = s:sub(i, i)
if char_set[char] then
result[char] = true
else
char_set[char] = true
end
end
end
```
该函数使用一个字符集来记录已经出现过的字符,如果当前字符已经在字符集中出现过,则将其添加到全局集result中。最后,函数返回全局集result。
python 完成函数find_rep_str,该函数接受字符串s,并查找所有重复的字符,这些字符是s的子字符串。子字符串应添加到全局集result中。
好的,以下是 Python 的代码实现:
```python
def find_rep_str(s):
result = set()
for i in range(len(s)):
for j in range(i+1, len(s)):
if s[i:j] in s[j:]:
result.add(s[i:j])
return result
```
这个函数会返回一个集合,其中包含了所有重复的子字符串。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)