-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
18 lines (17 loc) · 1.07 KB
/
Copy pathSolution.java
File metadata and controls
18 lines (17 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution
{
public String reverseStr(String s, int k)
{
if (s == null || s.length() == 0 || s.trim().length() == 0)
{
return s;
}
return reverse(s, k, 0, s.length());
} //Method to reverse and merge private String reverse(String s, int k, int start, int len) { if
// (start >= len) { return ""; } String result = ""; // check if there 'k'
// elements present, else reverse till the end of the string int end = Math.min(start + k, len); //
// Reverse and merge first k elements for (int i = end - 1; i >= start; i++) { result =
// result + s.charAt(i); } // Merge remaining k or left over elements in the same order // check
// if there 'k' elements present, else merge till the end of the string int end_leftOver = Math.min(end + k,
// len); for (int i = end; i < end_leftOver; i++) { result = result + s.charAt(i); }
// return result + reverse(s, k, start + 2 * k, len); }}