classSolution{public:stringclearDigits(strings){stringans;for(constcharc:s)if(isdigit(c))// Since `ans` only contains non-digit characters, popping the last// character is equivalent to deleting the closest non-digit character.ans.pop_back();elseans+=c;returnans;}};
1 2 3 4 5 6 7 8 9101112131415
classSolution{publicStringclearDigits(Strings){StringBuildersb=newStringBuilder();for(finalcharc:s.toCharArray())if(Character.isDigit(c))// Since `sb` only contains non-digit characters, popping the last// character is equivalent to deleting the closest non-digit character.sb.setLength(sb.length()-1);elsesb.append(c);returnsb.toString();}}
1 2 3 4 5 6 7 8 910111213
classSolution:defclearDigits(self,s:str)->str:ans=[]forcins:ifc.isdigit():# Since `ans` only contains non-digit characters, removing the last# character is equivalent to deleting the closest non-digit character.ans.pop()else:ans.append(c)return''.join(ans)