Input: s ="abc"Output: "abc"Explanation:
There is no digit in the string.
Example 2:
1
2
3
4
5
Input: s ="cb34"Output: ""Explanation:
First, we apply the operation on `s[2]`, and `s` becomes `"c4"`.Then we apply the operation on `s[1]`, and `s` becomes `""`.
classSolution {
public String clearDigits(String s) {
int i = 0;
StringBuilder sb =new StringBuilder(s);
// Until we reach the end of the stringwhile (i < sb.length()) {
if (Character.isDigit(sb.charAt(i))) {
// Remove the digit from the string sb.deleteCharAt(i);
// If there is a character to the left of the digit, remove itif (i > 0) {
sb.deleteCharAt(i - 1);
// Adjust the index to account for the removed character i--;
}
} else {
// Move to the next character if it's not a digit i++;
}
}
return sb.toString();
}
}
classSolution:
defclearDigits(self, s: str) -> str:
i: int =0 sb: list[str] = list(s)
# Until we reach the end of the stringwhile i < len(sb):
if sb[i].isdigit():
# Remove the digit from the listdel sb[i]
# If there is a character to the left of the digit, remove itif i >0:
del sb[i -1]
# Adjust index to account for the removed character i -=1else:
# Move to the next character if it's not a digit i +=1return''.join(sb)
⏰ Time complexity: O(n^2) in worst case. Because for each digit, performing an “erase” operation involves shifting characters, which has a time complexity of O(n). And in worst case, there will be n/2 digits in the string leading to time complexity of O(n^2).
🧺 Space complexity: O(n). The space complexity is linear due to the use of a StringBuilder in Java or a list in Python for storing the string.
classSolution {
public String clearDigits(String s) {
StringBuilder ans =new StringBuilder();
// Iterate through each character in the input stringfor (int i = 0; i < s.length(); i++) {
// if the current character is a digit and if the answer has charactersif (Character.isDigit(s.charAt(i)) && ans.length() != 0) {
// If true, remove the last character from the answer ans.setLength(ans.length() - 1);
} else {
// else if it is a char, add it to the answer ans.append(s.charAt(i));
}
}
return ans.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defclearDigits(self, s: str) -> str:
ans = []
# Iterate through each character in the input stringfor i in range(len(s)):
# if the current character is a digit and if the stack is not emptyif s[i].isdigit() and ans:
# If true, remove the last character from the stack ans.pop()
else:
# else if it is a char, add it to the stack ans.append(s[i])
return''.join(ans)
⏰ Time complexity: O(n). Each character in the string is processed once, and stack operations (push and pop) are O(1) operations. Therefore, the overall time complexity is linear.
🧺 Space complexity: O(n). The space complexity is linear due to using a stack-like structure to store the resulting characters.
Using a two-pointer technique allows for efficient in-place modification of the string. One pointer iterates through the string, and the other pointer keeps track of the position where the next valid character should be placed.
Here is the approach:
Use an array of characters to manipulate the string.
Traverse the string with one pointer i.
Maintain another pointer j that tracks the position of the next valid character in the result.
If a digit is found, decrement the pointer j (if it’s not already 0).
If a non-digit character is found, place it at the position j and increment j.
Finally, convert the mutable array back to a string using only the valid length determined by j.
publicclassSolution {
public String clearDigits(String s) {
int j = 0;
char[] ans = s.toCharArray();
// Iterate through each character in the input stringfor (int i = 0; i < s.length(); i++) {
// if the current character is a digit and there is at least one character in the resultif (Character.isDigit(s.charAt(i)) && j > 0) {
// If a digit, decrease j to effectively remove the previous character j = Math.max(j - 1, 0);
} else {
// else if it is a char, store it in the result array ans[j++]= s.charAt(i);
}
}
// Create a new string from the result array up to the valid lengthreturnnew String(ans, 0, j);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defclearDigits(self, s: str) -> str:
j: int =0 ans: list[str] = list(s)
# Iterate through each character in the input stringfor i in range(len(s)):
# if the current character is a digit and there is at least one character in the resultif s[i].isdigit() and j >0:
# If a digit, decrease j to effectively remove the previous character j = max(j -1, 0)
else:
# else if it is a char, store it in the result array ans[j] = s[i]
j +=1# Create a new string from the result array up to the valid lengthreturn''.join(ans[:j])