Skip to content

Commit c2f6be7

Browse files
committed
Have implemented the program to find the final string after removing all the occurrences of a substring:.
1 parent 4298bd4 commit c2f6be7

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

.idea/workspace.xml

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Remove All Occurrences of a Substring
3+
Link: https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
4+
*/
5+
package com.raj;
6+
7+
public class RemoveAllOccurrencesOfASubstring {
8+
public static void main(String[] args) {
9+
// Initialization.
10+
String s = "daabcbaabcbc";
11+
String part = "abc";
12+
StringBuilder ans = new StringBuilder(s);
13+
14+
// Logic.
15+
recursiveStringReplace(ans, part);
16+
17+
// Display the result.
18+
System.out.println("The final string after removing all the occurrences of a substring: " + ans+".");
19+
}
20+
21+
// Recursive call the function itself to replace the string with the part.
22+
private static StringBuilder recursiveStringReplace(StringBuilder s, String part) {
23+
for (int i = 0; i <= s.length() - part.length(); i++) {
24+
if (s.substring(i, i + part.length()).equals(part)) {
25+
s = s.delete(i, i + part.length());
26+
recursiveStringReplace(s, part);
27+
}
28+
}
29+
return s;
30+
}
31+
}

0 commit comments

Comments
 (0)