Problem:
Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive). You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".
withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"
Solution:
public String withoutString(String base, String remove) { int blen = base.length(); int rlen = remove.length(); String lowbase = base.toLowerCase(); String lowrem = remove.toLowerCase(); String fin = ""; for (int i = 0; i < blen; i++) { if (i <= blen - rlen) { String tmp = lowbase.substring(i,i+rlen); if (!tmp.equals(lowrem)) fin += base.substring(i,i+1); else { i += rlen-1; } } else { String tmp2 = lowbase.substring(i,i+1); if (!tmp2.equals(lowrem)) fin += base.substring(i,i+1); } } return fin; }
Can we do this(this works!)
ReplyDeletepublic String withoutString(String base, String remove) {
base=base.replace(remove.toUpperCase(),"");
base=base.replace(remove.toLowerCase(),"");
return base.replace(remove,"");
}
Fantastic approach
DeleteIt won't work for below input scenario
DeletewithoutString("THIS Is a FISH", "iS")
it worked perfectly!
Deleteyea i knew this solution but the goal for practise is to make a loop. https://codingbat.com/
Delete14 errors
ReplyDeleteTry this.
ReplyDeletepublic String withoutString(String base, String remove) {
String result1 = "";
String result2 = "";
String result3 = "";
String lowerCaseRemove = remove.toLowerCase();
String upperCaseRemove = remove.toUpperCase();
if(base.contains(remove)==false && base.contains(lowerCaseRemove)==false && base.contains(upperCaseRemove)==false){
return base;
}
if(base.contains(remove) || base.contains(lowerCaseRemove) || base.contains(upperCaseRemove)){
result1 = base.replaceAll(remove,"");
result2 = result1.replaceAll(lowerCaseRemove,"");
result3 = result2.replaceAll(upperCaseRemove,"");
}
return result3;
}
This works for me - i don't know if it is an elegant solution but it works -
ReplyDeletepublic String withoutString(String base, String remove) {
String baseL = base.toLowerCase();
String subStr;
int start = 0;
while(baseL.contains(remove.toLowerCase())){
start = baseL.indexOf(remove.toLowerCase());
subStr = base.substring(start, start+remove.length());
base = base.replace(subStr, "");
baseL = base.toLowerCase();
}
return base;
}
static String withoutString(String base, String remove) {
ReplyDeleteint baseL= base.length();
int remL = remove.length();
String result = "";
for(int i=0; i<baseL; i++){
if((i+remL-1)<baseL && base.substring(i, i+remL).toLowerCase().equals(remove.toLowerCase()) ){
base = base.substring(0, i)+base.substring(i+remL, baseL);
baseL= base.length();
i--;
}
}
return base;
}
how about that? :)
ReplyDeletepublic String withoutString(String base, String remove) {
String result="";
int b = base.length();
int r = remove.length();
for (int i=0; i<b;i++) {
if (i<b-r+1 && base.substring(i,i+r).toLowerCase().equals(remove.toLowerCase())) {
i=i+r-1;
continue;
}
result+=base.substring(i,i+1);
}
return result;
}
Ok when i solved this problem i was like ok this looks easy then i ended up doing this LOL wtf
ReplyDeletepublic String withoutString(String base, String remove) {
String newStr = "";
int len = base.length();
for(int i = 0; i < len - remove.length() + 1; i++){
if(base.length()>= remove.length()){
if(base.substring(0,remove.length()).equalsIgnoreCase(remove))
base = base.substring(remove.length());
else{
if(base.length() == remove.length()){
if(base.equalsIgnoreCase(remove))
base = base.substring(remove.length());
else{
newStr+= base;
break;
}
}
else
while(base.length() > remove.length() &&!base.substring(0,remove.length()).equalsIgnoreCase(remove)){
newStr+= base.charAt(0) +"";
base = base.substring(1);
}
}
}
}
if(base.length() < remove.length())
newStr += base;
return newStr;
}
Similar to the answer at the top of the page, except that I used the equalsIgnoreCase method to test for equality.
ReplyDeletepublic String withoutString(String base, String remove) {
String result = "";
int bl = base.length();
int rl = remove.length();
for(int i = 0; i < bl; i++){
if(i <= bl-rl){
String tmp = base.substring(i, i + rl);
if( tmp.equalsIgnoreCase(remove) ){
i += rl - 1;
} else {
result += base.charAt(i);
}
} else {
result += base.charAt(i);
}
}
return result;
}
public String withoutString(String base, String remove) {
ReplyDeleteint next = 0;
while ((next = base.toLowerCase().indexOf(remove.toLowerCase())) >= 0) {
base = base.substring(0, next) + base.substring(next + remove.length());
}
return base;
}
This is my solution to this problem. I think sometimes writing clean code worth efficiency. Even if we need that much efficiency, I think this code is easier to make faster.
public String withoutString(String base, String remove) {
ReplyDelete//use of Java pattern flag for case insensitive --- (?i)
String afterRemove = base.replaceAll("(?i)"+remove,"");
return afterRemove;
}
Awesome :)
Deletepublic String withoutString(String base, String remove) {
ReplyDeleteString x = "";
String base_new = base.toLowerCase();
String remove_new = remove.toLowerCase();
String remove_upper = remove.toUpperCase();
if(remove.length()==1) {
for(int i=0;i<base.length();i++) {
if(base_new.charAt(i) != remove_new.charAt(0)) {
x = x + base.charAt(i);
}
}
base = x;
}
else {
base = base.replace(remove,"");
base = base.replace(remove_new,"");
base = base.replace(remove_upper,"");
}
return base;
}
This might not be the point of the exercise but for anyone trying to solve with regex. (?i) is case-insensitive flag
ReplyDeletepublic String withoutString(String base, String remove) {
return base.replaceAll("(?i)"+remove,"");
}
public String withoutString(String base, String remove) {
ReplyDeletereturn base = base.replaceAll("(?i)" + remove, "");
}
I did it like this
public String withoutString(String base, String remove) {
ReplyDeleteint len = remove.length();
if(len > base.length()){
return base;
}
for( int i = 0 ; i < len ; i++){
if(base.substring(0,len).equalsIgnoreCase(remove) ){
return withoutString(base.substring(len), remove) ;
}else {
return base.charAt(0) + withoutString(base.substring(1), remove);
}
}
return base;
}
Just passing by, leaving this here:
ReplyDeletereturn base.replaceAll(remove,"").replaceAll(remove.toLowerCase(),"").replaceAll(remove.toUpperCase(),"");
public String withoutString(String base, String remove) {
ReplyDeleteint i;
while((i = base.toUpperCase().indexOf(remove.toUpperCase())) != -1){
base = base.substring(0, i) + base.substring(i+remove.length());
}
return base;
}
Recursive!
ReplyDeletepublic String withoutString(String base, String remove) {
if(base.length() == 0) return base;
if(base.length() >= remove.length()){
if(base.substring(0,remove.length()).equals(remove)
|| base.substring(0,remove.length()).equals(remove.toUpperCase())
|| base.substring(0,remove.length()).equals(remove.toLowerCase()))
return withoutString(base.substring(remove.length()), remove);
}
return base.charAt(0) + withoutString(base.substring(1), remove);
}
Shortest way!! it works.
ReplyDeletepublic String withoutString(String base, String remove) {
String k = String.format("(?i)%s", remove);
return (base.replaceAll(k, ""));
}
public String withoutString(String base, String remove) {
ReplyDeleteString ret = "";
for (int i = 0; i < base.length(); i++)
{
if (i + remove.length() <= base.length() && base.substring(i, i + remove.length()).equalsIgnoreCase(remove))
{
i += remove.length() - 1;
}
else
{
ret += base.charAt(i);
}
}
return ret;
}
public String withoutString(String base, String remove) {
ReplyDelete// eliminate all instances of the "remove" string
return base
.replace(remove.toUpperCase(),"")
.replace(remove.toLowerCase(),"")
.replace(remove,"");
}
public String withoutString(String base, String remove) {
ReplyDeletereturn base
.replace(remove.toUpperCase(),"")
.replace(remove.toLowerCase(),"")
.replace(remove,"");
}
public String withoutString(String base, String remove) {
ReplyDeleteString lowerRemove = remove.toLowerCase();
String upperRemove = remove.toUpperCase();
for(int i=0; i<base.length(); i++){
if(base.substring(i).startsWith(lowerRemove) || base.substring(i).startsWith(upperRemove) || base.substring(i).startsWith(remove)){
base = base.replace(base.substring(i,i+remove.length()),"");
}
}
return base;
}
return base.replaceAll("(?i)"+remove,"");
ReplyDeletefunction withoutString(m,n){
ReplyDeletevar x = m.replaceAll(n,'');
console.log(x);
}
withoutString("Hello there","llo");
function withoutString(m,n){
ReplyDeletevar x = m.replaceAll(n,'');
console.log(x);
}
withoutString("Hello there","llo");
This is by Using Javascript
Recursive Solution:
ReplyDeletepublic String withoutString(String base, String remove) {
if (base.length() < remove.length()) {
return base;
}
if (base.substring(0, remove.length()).toLowerCase().equals(remove.toLowerCase())) {
return withoutString(base.substring(remove.length()), remove);
}
return base.charAt(0) + withoutString(base.substring(1), remove);
}
public String withoutString(String base, String remove) {
ReplyDeleteString res = "";
String sub = "";
int i;
do {
i = base.toLowerCase(Locale.ROOT).indexOf(remove.toLowerCase(Locale.ROOT));
if (i != -1) {
res = base.substring(0, i);
res = res + sub;
res = res + base.substring(i + remove.length());
base = res;
}
} while (i != -1);
return base;
}
public String withoutString(String base, String remove) {
ReplyDeletereturn base.replaceAll("(?i)" + remove, "");
}
public String withoutString(String base, String remove) {
ReplyDeleteString str = "";
str = base.replace(remove, "");
remove = remove.toUpperCase(Locale.ROOT);
str = str.replace(remove, "");
remove = remove.toLowerCase(Locale.ROOT);
str = str.replace(remove, "");
return str;
}
public String withoutString(String base, String remove) {
ReplyDeleteString base1 = base.toLowerCase();
String remove1 = remove.toLowerCase();
String res = "";
for(int i = 0; i<base.length(); i++) {
if(!base1.startsWith(remove1, i)) {
res = res + base.substring(i, i+1);
} else {
i += remove.length()-1;
}
}
return res;
}