Question
Those methods fail for some of the tests, could you fix it? Source code that fails: public static String replacement(String ori, String r, String w,
Those methods fail for some of the tests, could you fix it?
Source code that fails:
public static String replacement(String ori, String r, String w, int N){ //TODO if(null==ori) return null; if(r==null || w==null) return ori;
int fromIndex=0, rCount = 0; while((fromIndex = ori.indexOf(r, fromIndex))!=-1) { rCount++; fromIndex++; }
float min = N; if(min>rCount) min = rCount;
if(min*w.length()/ori.length()>0.45) { String tmp = r; r = w; w = tmp; }
int lastIndex = ori.lastIndexOf(r); String final_str = "";
while(N>0 && lastIndex!=-1) { final_str = w + ori.substring(lastIndex+r.length(), ori.length()) + final_str; ori = ori.substring(0, lastIndex); lastIndex = ori.lastIndexOf(r); N--; } final_str = ori + final_str; return final_str;
}
Implement the following method: COPYRIGHT public static String replacement(String ori, String r, String w, int n) COPYRIGE ing replacement (strin This method should replace upto the last N occurrences of the String r with String w if the total length of String w (number of occurrence of w in the resulting String X w.length() is less than 45% (inclusive) of the length of ori String; otherwise, you are going to replace upto the last N occurrences of w with r. If r or w is null or an empty String, return original String without any modification. If original String is null, return null. Otherwise, return a String after applying these rules. For example, CC replacement("Skiing is fun","i","ii", 3) should return "Sking is fun" replacement("Skiing is fun","i","ii", 1) should return "Sking iis fun" replacement("AAABB","A","C",1) should return "AACBB" replacement("AAABB","A","C",2) should return "ACCBB" replacement("AAABB","A","B",3) should return "AAAAA" this problem IGHT Please use a comment line to record how long time you spent on this problem 1 replacement ("I like music", "music","like", 2) I music music I like like 2 3 replacement ("aaaa bbbbb gggg replacement (null,"A", "B",3) replacement (, "A", "B",3) cccccc dddddd aaaaa","a","d",5) aaaa bbbbb gggg null cccccc dddddd ddddd aaaa bbbbb gggg null Failed cccccc dddddd ddddd Pass Pass 4 Pass 5 replacement ("aaaaa cccccc dddddd aaaaa", "C", "a", 2) aaaaa cccccc dddddd aaacc aaaaa ccccaa dddddd aaaaa Failed 6 replacement ("I am still trying", "still", "trying",3) I am still still I am trying trying Failed 7 replacement ("I am OK. I got it!", "am OK","got it!",3) I am OK. I am OK I got it!. I got it! Failed 8 Pass replacement ("Skiing is fun", null, "ii", 3) replacement ("Skiing is fun", hello, "",3) replacement ("Skiing is fun","i","ii",3) 9 10 Skiing is fun Skiing is fun Sking is fun Skiing is fun Skiing is fun Sking is fun Pass Pass Implement the following method: COPYRIGHT public static String replacement(String ori, String r, String w, int n) COPYRIGE ing replacement (strin This method should replace upto the last N occurrences of the String r with String w if the total length of String w (number of occurrence of w in the resulting String X w.length() is less than 45% (inclusive) of the length of ori String; otherwise, you are going to replace upto the last N occurrences of w with r. If r or w is null or an empty String, return original String without any modification. If original String is null, return null. Otherwise, return a String after applying these rules. For example, CC replacement("Skiing is fun","i","ii", 3) should return "Sking is fun" replacement("Skiing is fun","i","ii", 1) should return "Sking iis fun" replacement("AAABB","A","C",1) should return "AACBB" replacement("AAABB","A","C",2) should return "ACCBB" replacement("AAABB","A","B",3) should return "AAAAA" this problem IGHT Please use a comment line to record how long time you spent on this problem 1 replacement ("I like music", "music","like", 2) I music music I like like 2 3 replacement ("aaaa bbbbb gggg replacement (null,"A", "B",3) replacement (, "A", "B",3) cccccc dddddd aaaaa","a","d",5) aaaa bbbbb gggg null cccccc dddddd ddddd aaaa bbbbb gggg null Failed cccccc dddddd ddddd Pass Pass 4 Pass 5 replacement ("aaaaa cccccc dddddd aaaaa", "C", "a", 2) aaaaa cccccc dddddd aaacc aaaaa ccccaa dddddd aaaaa Failed 6 replacement ("I am still trying", "still", "trying",3) I am still still I am trying trying Failed 7 replacement ("I am OK. I got it!", "am OK","got it!",3) I am OK. I am OK I got it!. I got it! Failed 8 Pass replacement ("Skiing is fun", null, "ii", 3) replacement ("Skiing is fun", hello, "",3) replacement ("Skiing is fun","i","ii",3) 9 10 Skiing is fun Skiing is fun Sking is fun Skiing is fun Skiing is fun Sking is fun Pass PassStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started