Problems
Problem 1
// This works well for smaller strings. However, it involves creating multiple substrings
// and performing string concatenation, which can be inefficient for large strings
// due to the immutable nature of String. Each substring and concatenation creates new objects,
// leading to higher memory usage and computational overhead.
String left = s.substring(0,L);
String mid = new StringBuffer(s.substring(L,R+1)).reverse().toString();
String right = s.substring(R+1);
String output = left + mid + right;//Better Solution
StringBuffer sb = new StringBuffer(s);
while (L < R) {
// Swap characters at L and R
char temp = sb.charAt(L);
sb.setCharAt(L, sb.charAt(R));
sb.setCharAt(R, temp);
L++;
R--;
}
String output = sb.toString();Last updated