-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveAllx.java
40 lines (32 loc) · 953 Bytes
/
moveAllx.java
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
32
33
34
35
36
37
38
39
40
public class moveAllx {
public static void shiftx(String str,int index, int count, String newString){
//base case
if(index== str.length()-1){
for(int i=0; i<count; i++){
newString+='x';
}
System.out.println(newString);
return ;
}
char currChar = str.charAt(index);
if(currChar =='x'){
count++;
shiftx(str,index+1,count,newString);
}else{
newString+=currChar;
shiftx(str, index+1, count, newString);
}
}
public static void main(String []args){
String str="axbcxxd";
shiftx(str, 0, 0, "");
}
}
/**move all 'x' to the end of the string
* base case :
* string length complete
* working :
* input a stringBuilder, input index
* if x encountered , shift to the end of the string builder
* check for th next index
*/