简体   繁体   中英

Find unique substring of String s with given length

I am trying to solve this question. For example, the string equals "caaab" and the length K=2, the return substrings should be "aa","ab","ca" in an alphabetic order. I prefer the solution in python

Try this:

input_str = 'caaab'
k = 2

unique_substrings = set(input_str[i: i + k] for i in range(len(input_str) - k + 1))
sorted_substrings = sorted(unique_substrings)

print(sorted_substrings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM