a) Write an algorithm in pseudo-code (in file h2.doc) in a function find dup_str
ID: 3902386 • Letter: A
Question
a) Write an algorithm in pseudo-code (in file h2.doc) in a function find dup_str(s, n) that determines whether a string s contains a duplicated substring of a given length n and that returns the first occurring substring of length n that is duplicated (if any) or the empty string, otherwise. For the example above (s-"abcdefbcdgh") with substring length 3, the substring to be returned is "bcd". With length 2, the algorithm returns "bc", and not "cd". For this value of parameter s, if the length parameter is 4 or more, the algorithm returns ""Explanation / Answer
Pseudocode -
Consider the length of the string be stored in variable "size".I will consider the string starting from index 1
Start a loop from 1 till size-n do
Make a substring of length "n" and store it in variable named "s1"
Now loop from value of outer loop+1 till size-n do
Make a substring of length "n" and store it in variable named "s2"
check if s1 = s2 then
return s1 (This is the first duplicate substring of length n)
done
done
return NULL (indicating that no duplicate substring of length n is present)
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.