Hello @Rehan_Butt,
Although the code provided by @HARVEER_SINGH looks clean and is straightforward but after critically analyzing the code that you’ve shared, I found that it will not work correctly on a few test cases. For example, if we have a string “TTTT”, or “AAA” (a string containing the same character), with k
always greater than the string length.
input string = “TTTT”
k = 7
The provided solution code throws an error saying “Error occurred while processing: runtime error: index out of range [4] with length 4
.” To fix this, you will need to add a check at the beginning of the code:
n := len(s)
if n < k {
return *NewSet()
}
The significance of these lines is to handle cases where the input string is too short to form substrings of the desired length (k
). In such a scenario, finding repeated sequences is not possible, so the function returns an empty set as no repeated sequences can be found. This prevents further execution of the function for cases where the input is insufficient for the specified substring length.
@HARVEER_SINGH I hope this will help in understanding how your solution code acts in certain circumstances, also let me know if you need more details or comparison between the solution code provided in the lesson and your solution code.
As @Rehan_Butt we really appreciate when learners share their feedback on our courses and likewise, we love to help you out for better user experience. Feel free to share more suggestions and feedbacks. Thanks!
Happy learning!