public static String switchString(String word, int i, int j) { StringBuilder newString = new StringBuilder(); newString.append(word.substring(j, word.length())); newString.append(word.substring(i + 1, j)); newString.append(word.substring(0, i + 1)); return newString.toString(); } For a given input String and indexes i and j, the code should do something like this: if word = “abcdef” and i = 1, j = 3. Output should be: “defcab”.
Assuming that the word is not null and i and j are not out of bounds, the code works fine. I want to know if, for a string of any given length, does this program take constant space in memory via this approach.
OutOfRangeexception or someting like this atnewString.append(word.substring(j, word.length()));nmemory.