Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f961e8d

Browse files
Add implementations for moving all 'x' characters and removing duplicates from strings
1 parent 1f7d457 commit f961e8d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎Recursion/MoveAllXString.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class MoveAllXString {
2+
public static void main(String[] args) {
3+
String str = "axbcxxd";
4+
skip(0, "", str);
5+
// Output: abcd
6+
// Explanation: All 'x' characters are removed from the string.
7+
}
8+
9+
static void skip(int index, String newStr, String str){
10+
if(index == str.length()){
11+
System.out.println(newStr);
12+
return;
13+
}
14+
15+
char currentChar = str.charAt(index);
16+
if(currentChar == 'x'){
17+
skip(index+1, newStr, str);
18+
}else{
19+
newStr +=currentChar;
20+
skip(index+1, newStr, str);
21+
}
22+
}
23+
}

‎Recursion/RemoveDuplicatsString.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class RemoveDuplicatsString {
2+
public static void main(String[] args) {
3+
String str = "abbccddeeffa";
4+
removeDuplicatesFun(0, "", str);
5+
6+
}
7+
8+
static void removeDuplicatesFun(int index, String newStr, String str) {
9+
if (index == str.length()) {
10+
System.out.println(newStr);
11+
return;
12+
}
13+
14+
char currentChar = str.charAt(index);
15+
16+
if (newStr.indexOf(currentChar) != -1) { // Character already exists - skip it (don't add duplicate)
17+
removeDuplicatesFun(index + 1, newStr, str);
18+
} else {
19+
newStr += currentChar;
20+
removeDuplicatesFun(index + 1, newStr, str);
21+
}
22+
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /