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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!