Find unique substring of String s with given length

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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





Might you prefer to think about the solution in python before posting here as well?
– domochevski
Aug 10 at 22:28




1 Answer
1



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)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard