Problem:
Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.
plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"
Solution:
public String plusOut(String str, String word) { int slen = str.length(); int wlen = word.length(); String fin = ""; for (int i = 0; i < slen; i++) { if (i <= slen - wlen) { String tmp = str.substring(i,i+wlen); if (tmp.equals(word)) { fin += word; i += wlen-1; } else fin += "+"; } else fin += "+"; } return fin; }
can you please comment about the efficiency of this code:(good or bad)
ReplyDeletepublic String plusOut(String str, String word) {
if(str.contains(word)){
str=str.replace(word,"--");
for(int i=0;i<str.length();i++){
if(str.charAt(i)!='-')
str=str.replace(str.substring(i,i+1),"+");
}
}
str=str.replace("--",word);
return str;
}
I like your solution, it is very clear.
DeleteNice solution, yea, BUT! If String str contains some "-" at first place? It will make wrong results!
Deletecan someone comment on this
ReplyDeletepublic String plusOut(String str, String word) {
StringBuilder result = new StringBuilder ("");
int count = 0;
int jump = word.length();
for (int i = 0; i <str.length()-jump+1;i++){
if (str.substring(i,i+word.length()).equals(word) && count <1) {
result.append(word);
i = i+jump-1;
}
else result.append("+");
}
if (!word.substring(jump-1).equals(str.substring(str.length()-1))) {
for (int i2 = 0; i2<jump-1;i2++){
result.append("+");
}
}
return result.toString();
}
Can anyone comment on this? I wanted to avoid making another variable.
ReplyDeleteMy idea was that I only had to check for a word in the first place if str.charAt(currentIndex) was equal to the first letter of the word.
public String plusOut(String str, String word) {
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == word.charAt(0)) {
if ((str.substring(i, i + word.length()).equals(word))) {
i += word.length() -1; // ++i runs right after this.
} else str = str.substring(0, i) + "+" + str.substring(i + 1, str.length());
}
else str = str.substring(0, i) + "+" + str.substring(i + 1, str.length());
}
return str;
}
public String plusOut(String str, String word) {
ReplyDeletefor(int i=str.lastIndexOf(word)+word.length();i<str.length();i++) {
str=str.replace(str.substring(i,i+1), "+");
}
int start=0;
while (start < str.length()) {
//System.out.println(start);
if (str.indexOf(word,start) == -1)
return str;
int id = str.indexOf(word, start);
String sub = str.substring(start,id), plus="";
//System.out.println(sub);
for(int j=0; j<sub.length(); j++) { plus+="+"; }
//System.out.println("sub " + sub + " TBR with " + plus);
str=str.replace(sub,plus);
start= id+word.length();
//System.out.println(start);
}
return str;
}
public String plusOut(String str, String word) {
ReplyDeleteif (str.length() == 0) return "";
return str.length() >= word.length() && str.substring(0, word.length()).equals(word) ? word + plusOut(str.substring(word.length(), str.length()), word):"+"+plusOut(str.substring(1, str.length()), word);
}
This is a nice solution:
ReplyDeletepublic String plusOut(String str, String word) {
String plus = "", newStr = "";
while(str.length()>=word.length()){
if(!str.substring(0,word.length()).equals(word)){
newStr+= "+";
str = str.substring(1);
}
else{
newStr += word;
str = str.substring(word.length());
}
}
for(int i = 0; i < str.length(); i++)
newStr+= "+";
return newStr;
}
My solution is similar to the one at the top of the page (and not as elegant, I must admit). What I found tricky about this problem is that the variable, i, will increment by 1 if we are merely replacing a given character with a "+", but it will increment by the length of the target word if we encounter the target. So, I created a variable, pos, to keep track of the position of i.
ReplyDeletepublic String plusOut(String str, String word) {
String result = "";
int sl public String plusOut(String str, String word) {
String result = "";
int sl = str.length();
int wl = word.length();
int pos = 0;
for(int i = pos; i <= sl - wl; i++){
if(str.substring(i, i + wl).equals(word)){
result += word;
i += wl - 1;
pos += word.length();
} else {
result += "+";
pos++;
}
}
for(int i = pos; i < str.length(); i++){
result += "+";
}
return result;
}= str.length();
int wl = word.length();
int pos = 0;
for(int i = pos; i <= sl - wl; i++){
if(str.substring(i, i + wl).equals(word)){
result += word;
i += wl - 1;
pos += word.length();
} else {
result += "+";
pos++;
}
}
for(int i = pos; i < str.length(); i++){
result += "+";
}
return result;
}
Wow...that got screwed up. Correct code is:
Deletepublic String plusOut(String str, String word) {
String result = "";
int sl = str.length();
int wl = word.length();
int pos = 0;
for(int i = pos; i <= sl - wl; i++){
if(str.substring(i, i + wl).equals(word)){
result += word;
i += wl - 1;
pos += word.length();
} else {
result += "+";
pos++;
}
}
for(int i = pos; i < str.length(); i++){
result += "+";
}
return result;
}
public String plusOut(String str, String word) { //13 && 2
ReplyDeleteString result = "";
for (int i=0 ; i<=str.length()-word.length(); i++) {
if (!str.substring(i,i+word.length()).equals(word)) {
result = result + "+";
}
else {
result = result + word;
i=i+word.length()-1;
}
}
for (int a = 1; a<word.length(); a++) {
if (!str.endsWith(word)) {
result = result + "+";
}
}
return result;
}
public String plusOut(String str, String word) {
ReplyDeleteString result="";
for(int i=0;i<str.length();i++)
{
if(i<=str.length()-word.length()){
if(word.equals(str.substring(i,i+word.length()))){result+=word;i+=word.length()-1;}else
result+="+";}
else
result+="+";
}return result;
}
public String plusOut(String str, String word) {
ReplyDeleteString result = "";
String temp = "";
int j = 0;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) != word.charAt(j)){
result = result + "+";
if(temp.length()!=0){
for(int k = 0; k < temp.length(); k++){
result = result + "+";
}
}
temp = "";
j = 0;
} else {
temp = temp + str.charAt(i);
if(word.length() == temp.length()){
result = result + temp;
j = 0;
temp = "";
} else {
j++;
}
}
}
return result;
}
public String plusOut(String str, String word) {
ReplyDeleteString output = "";
int sl = str.length();
int wl = word.length();
for (int i = 0; i < sl; i++) {
if ((i <= sl - wl) && (str.substring(i , i + wl).equals(word))) {
output += word;
i += wl - 1;
continue;
}
output += "+";
}
return output;
}
With the Bowflex TreadClimber, you can change the height and the intensity quite easily. treadclimber
ReplyDeletepublic String plusOut(String str, String word) {
ReplyDeleteString change = "";
for (int i = 0; i < str.length(); i++)
{
if (i + word.length() <= str.length() && str.substring(i, i + word.length()).equals(word))
{
change += word;
i += word.length() - 1;
}
else
{
change += '+';
}
}
return change;
}
Essentially, the Bowflex TreadClimber allows you to use a combination of moves to get the right kind of effect. treadclimber reviews
ReplyDeletepublic String plusOut(String str, String word) {
ReplyDeleteint i=0;
String ret="";
while(i<str.length()){
if(str.substring(i).startsWith(word)){
ret += word;
i += word.length();
}else{
ret += "+";
i++;
}
}
return ret;
}
public String plusOut(String str, String word) {
ReplyDeleteif(str.contains(word)){
str=str.replace(word,"--");//بدل كل الحروف المطلوب البحث عنتها هنا بسالب مثلا او أي رمز
for(int i=0;i<str.length();i++){
if(str.charAt(i)!='-')//لو ملقتهوش
str=str.replace(str.substring(i,i+1),"+");//حط اللي بتختبره بالرمز المطلوب
}
}
str=str.replace("--",word);//بدل كل الرموز السالب بالوورد تاني
return str;
}
/*str=str.replaceAll(word,"00");
for(int i=0 ; i<str.length() ; i++){
if(str.charAt(i)!='0' ) str=str.replace(str.substring(i,i+1),"+");
}
str=str.replaceAll("0",word);
return str;
}*/
public static String plusOut(String str, String word) {
ReplyDeleteint lenWord = word.length();
String result = "";
for(int i = 0; i < str.length()-lenWord; i++){
if(!str.substring(i,i+lenWord).equals(word)){
result += "+";
} else{
result += str.substring(i,i+lenWord);
i+=lenWord-1;
}
}
int dif = str.length() - result.length();
if(str.endsWith(word)){
result += word;
} else {
for(int i = 0; i< dif; i++){
result+="+";
}
}
return result;
}
public static void plusout(String a,String b) {
ReplyDeleteString ans = "";
for(int i=0;i<a.length();i++) {
if(!b.contains(a.charAt(i)+"")) {
ans = ans + "+";
}else {
ans = ans + a.charAt(i);
}
}
System.out.println(ans);
}
public String plusOut(String str, String word) {
ReplyDeleteString result = "";
int position = 0;
for (int i = 0; i < str.length(); i++) {
if (i == str.indexOf(word, position)){
result = result + word;
position = i + word.length();
i += word.length()-1;
}
else {
result = result + "+";
}
}
return result;
}
public String plusOut(String str, String word) {
ReplyDeleteString s = str.replace(word, "*");
s = s.replaceAll("([^\\*])", "+");
return s.replace("*", word);
}