Question
We wish to implement a method String insert(char c, String s) that returns a string with c inserted in the correct position in already sorted
We wish to implement a method String insert(char c, String s) that returns a string with c inserted in the correct position in already sorted String s. To do so we will implement insert as follows:
static String insert(char c, String s) {
return insertHelper(c, "", s);
}
static String insertHelper(char c, String left, String right) {} // strip leading characters from right, & append to left 'till insertion point found. Now return left + c + right.
For example, insert('e', "bcdfg") would call
insertHelper('e', "", "bcdfg") which would recursively call
insertHelper('e', "b" , "cdfg") followed by
insertHelper('e', "bc" , "dfg") followed by
insertHelper('e', "bcd", "fg).
This last call would then return "bcd" + 'e' + "fg" which is gives the final result "bcdefg".
IMPLEMENT insertHelper. (Don't implement insert() )
Your implementation of insertHelper must be tailRecursive.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started