-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram66.java
More file actions
31 lines (25 loc) · 955 Bytes
/
Program66.java
File metadata and controls
31 lines (25 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Program66 {
//Move all'x' to the end of the string -> "axbcxxd"
public static void moveAllX(String str, int idx, int count, String newString) {
if (idx == str.length()) {
for (int i = 0; i < count; i++) {
newString += 'x';
}
System.out.println(newString);
return;
}
char currChar = str.charAt(idx);
if (currChar == 'x') {
count++;
moveAllX(str, idx + 1, count, newString);
} else {
newString += currChar;//newString = newString+currChar
moveAllX(str, idx + 1, count, newString);
}
}
public static void main(String[] args) {
String str = "axbcxxd";
moveAllX(str, 0, 0, "");
}
}
// time complexity : traversing= O(n) + recursive calls O(n)-> O(2n) = O(n) [in assymptotic notation we remove the constant factor]