Problem:
Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly. On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1.
startWord("hippo", "hi") → "hi"
startWord("hippo", "xip") → "hip"
startWord("hippo", "i") → "h"
Solution:
public String startWord(String str, String word) { int lenStr = str.length(); int lenWord = word.length(); if (lenStr == 0) return ""; if (lenWord > lenStr) return ""; if (word.length() == 1) return str.substring(0,1); else if (str.substring(1,lenWord).equals(word.substring(1,lenWord))) return str.substring(0,lenWord); else return ""; }
what if (lenStr) is smaller than (lenWord) wouldnt it give a error [index out of bound...]
ReplyDeletepublic String startWord(String str, String word) {
ReplyDeleteString match = "";
if ( !str.isEmpty()
&& str.length() >= word.length()
&& str.substring(1, word.length()).equals
(word.substring(1,word.length()))) {
match = str.substring(0,word.length());
}
return match;
}
public String startWord(String str, String word) {
ReplyDeleteif(str.length()>=word.length()
&& str.substring(1,word.length()).equals(word.substring(1,word.length())))
return str.substring(0,word.length());
else return "";
}
public String startWord(String str, String word) {
ReplyDeleteint wlen = word.length();
int slen = str.length();
if ((slen<1)||(wlen>slen))
return "";
if (str.substring(1,wlen).equals(word.substring(1,wlen)))
return str.substring(0,1)+word.substring(1);
else return "";
}
public String startWord(String str, String word) {
ReplyDeleteif(word.length()>str.length()){
return "";
}
String result = String.valueOf(str.charAt(0));
for(int i = 1; i < word.length(); i++) {
if(str.length()!=0 && word.charAt(i) == str.charAt(i)){
result = result+ word.charAt(i);
} else {
result = "";
}
}
return result;
}
public String startWord(String str, String word) {
ReplyDeleteif(str.length() >= word.length()) {
boolean startsWithWord = str.startsWith(word);
if(startsWithWord)
return word;
if(str.charAt(0) != word.charAt(0) &&
str.substring(1, word.length()).equals(word.substring(1))) {
return str.substring(0, word.length());
}
}
return "";
}
if (str.indexOf(word) >= 0 && word.equals(str.substring(0, word.length())))
ReplyDeletereturn word;
else if (str.length() >= 1 && str.indexOf(word.substring(1)) >= 0 && !(word.equals(str.substring(0,word.length()))))
return str.substring(0, word.length());
else
return "";
if(str.length()>0) {
ReplyDeleteif (str.startsWith(word) || str.substring(1).startsWith(word.substring(1))) {
return str.substring(0, word.length());
}
}
return "";
public String startWord(String str, String word) {
ReplyDeleteif(word.length()>str.length()){
return "";
}
else if(str.substring(0,word.length()).equals(word) || str.substring(1,word.length()).equals(word.substring(1,word.length()))){
return str.substring(0,word.length());
}
else{
return "";
}
}
public String startWord(String str, String word) {
ReplyDeleteif(str.length()>0) {
if (str.startsWith(word) || str.substring(1).startsWith(word.substring(1))) {
return str.substring(0, word.length());}
}
return "";
}
Great Job!!
Deletepublic String startWord(String str, String word) {
ReplyDeleteString ste = "";
if (str.length() < word.length()) return "";
if (word.length() > 1) {
String tester = word.substring(1, word.length());
if (str.contains(tester)) {
ste = str.substring(0, word.length());
}
} else {
ste = str.substring(0, word.length());
}
return ste;
}
int wLen = word.length();
ReplyDeleteint sLen = str.length();
if (sLen >= wLen && str.substring(1, wLen).equals(word.substring(1, wLen)))
return str.substring(0, wLen);
return "";
My Try:
ReplyDeletepublic String startWord(String str, String word) {
int len_word = word.length();
if(len_word > str.length()) {
return "";
}
if(str.substring(1, len_word).equals(word.substring(1))){
return str.substring(0, len_word);
}
return "";
}