Problem:
Given a string and a non-empty word string, return a string made of each char just before and just after every appearance of the word in the string. Ignore cases where there is no char before or after the word, and a char may be included twice if it is between two words.
wordEnds("abcXY123XYijk", "XY") → "c13i"
wordEnds("XY123XY", "XY") → "13"
wordEnds("XY1XY", "XY") → "11"
Solution:
public String wordEnds(String str, String word) { int slen = str.length(); int wlen = word.length(); String fin = ""; for (int i = 0; i < slen-wlen+1; i++) { String tmp = str.substring(i,i+wlen); if (i > 0 && tmp.equals(word)) fin += str.substring(i-1,i); if (i < slen-wlen && tmp.equals(word)) fin += str.substring(i+wlen,i+wlen+1); } return fin; }
Nice solution, cleaner than what I had(which did work), thanks for posting.
ReplyDeleteAlternatively:
ReplyDeletepublic String wordEnds(String str, String word) {
int strLen = str.length();
int wordLen = word.length();
String result = "";
for (int i = 0; i < strLen-wordLen+1; i++) {
if (i > 0 && str.substring(i,i+wordLen).equals(word))
result += str.charAt(i-1);
if (i < strLen-wordLen && str.substring(i,i+wordLen).equals(word))
result += str.charAt(i+wordLen);
}
return result;
}
logically this code should produce aiobe exception.
DeleteI know it looks big but it is easy if u read it from top to bottom
ReplyDeletepublic String wordEnds(String str, String word) {
String newStr = "";
if(str.length() < 2 || word.length() == 0)
return "";
str = str.replace(word, ".");
if(str.length() >= 2){
for(int i = 1; i < str.length()-1; i++){
if(str.charAt(i) != '.'){
if(str.charAt(i-1) =='.')
newStr += str.charAt(i);
if(str.charAt(i+1) == '.')
newStr += str.charAt(i);
}
if(str.charAt(i) == '.'){
if(str.charAt(i-1) =='.')
newStr += word;
if(str.charAt(i+1) == '.')
newStr += word;
}
}
if(str.charAt(0) != '.')
if(str.charAt(1) == '.')
newStr= str.charAt(0)+ "" + newStr;;
if(str.charAt(str.length()-1) != '.')
if(str.charAt(str.length()-2) == '.')
newStr= newStr+""+ str.charAt(str.length()-1);
if(str.charAt(str.length()-1) == '.')
if(str.charAt(str.length()-2) == '.')
newStr= newStr+""+ word;
}
return newStr;
}
public String wordEnds(String str, String word) {
ReplyDeleteString result = "";
int l = word.length();
for (int i = 0; i < str.length(); i++) {
if (str.startsWith(word, i)) {
result += ( (i-1) >= 0 ) ? str.charAt(i-1) : "";
result += ( (i+l) < str.length() ) ? str.charAt(i+l) : "";
}
}
return result;
}
use if statement in place of trintry operation..
My solution is very similar to the one at the top of the page, except that I didn't use a temporary String variable in the for loop. Here's my logic:
ReplyDeleteIf i is greater than zero, and i is positioned at the start of the target word, add the preceding character to the result string. If i is less than the length of the string minus the target word length, and i is positioned at the start of the target word, add the character immediately following the target word.
public String wordEnds(String str, String word) {
String result = "";
int sl = str.length();
int wl = word.length();
for(int i = 0; i <= sl - wl; i++){
if(i >= 1 && str.substring(i, i+wl).equals(word)){
result += str.charAt(i-1);
}
if(str.substring(i, i+wl).equals(word) && i < sl-wl ){
result += str.charAt(i+wl);
}
}
return result;
}
public String wordEnds(String str, String word) {
ReplyDeleteString result="";
for(int i=0;i0)result+=str.charAt(i-1);
if(i<str.length()-word.length())result+=str.charAt(i+word.length());
}
}return result;
}
explain for loop
Deletepublic String wordEnds(String str, String word) {
ReplyDeleteString result = "";
if (str.equals(word)){
return result;
}
if (str.startsWith(word)) {
result += str.charAt(word.length());
}
for (int i = 1; i < str.length() - word.length(); i++) {
if (str.startsWith(word, i)) {
result += str.charAt(i - 1);
result += str.charAt(i + word.length());
}
}
if (str.endsWith(word)){
result+= str.charAt(str.length()- word.length()-1);
}
return result;
}
I got infra or precheck error what to do
ReplyDeletegand me daal
Deletepublic String wordEnds(String str, String word) {
ReplyDeleteint len=word.length();
String result="";
for(int i=0;i0 && temp.equals(word))
{
result=result+str.substring(i-1,i)+str.substring(i+len,i+len+1);
}
}else{
String temp1 =str.substring(i);
if(str.length()>2 && temp1.equals(word))
result=result+str.substring(i-1,i);
}
}
return result;
}
public String wordEnds(String str, String word) {
ReplyDeleteint word_length = word.length();
String result = "";
if(word_length == str.length()) return "";
for(int i = 0; i <= str.length() - word_length; i++){
if(str.substring(i,i+word_length).equals(word)){
if(i == 0){
result += str.substring(word_length,word_length+1);
}
else if(i == (str.length() - word_length)){
result += str.substring(str.length() - word_length - 1,str.length() - word_length);
}
else{
result += (str.substring(i-1,i) + str.substring(i+word_length,i+word_length+1));
}
}
}
return result;
}
public String wordEnds(String str, String word) {
ReplyDeleteString ends = "";
for (int i = 0; i <= str.length() - word.length(); i++)
{
if (str.substring(i, i + word.length()).equals(word))
{
if (i - 1 >= 0)
{
ends += str.charAt(i - 1);
}
if (i + word.length() < str.length())
{
ends += str.charAt(i + word.length());
}
i += word.length() - 1;
}
}
return ends;
}
public String wordEnds(String str, String word) {
ReplyDeletereturn str.replaceAll(".*?(?="+word+")(?<=(.|^))"+word+"(?=(.|$))|.+","$1$2");
}
wow
Deletepublic String wordEnds(String str, String word) {
ReplyDeleteStringBuilder newStr = new StringBuilder();
for(int i = 0; i < str.length()-word.length()+1; i++){
if(i > 0 && str.substring(i,i+word.length()).equals(word))
newStr.append(str.charAt(i-1));
if(i < str.length()-word.length() && str.substring(i,i+word.length()).equals(word))
newStr.append(str.charAt(i+word.length()));
}
return newStr.toString();
}
public String wordEnds(String str, String word) {
ReplyDeleteString newstr = "";
int index = 0;
int deender = 0;
int tester = 100;
if(str.equals(word))return newstr;
for (int i = 0; i < str.length(); i++) {
index = str.indexOf(word, deender);
if (index != tester && index >= 0) {
tester = index;
if (index == 0) {
newstr += str.charAt(index + word.length());
deender += i + 1;
} else if (index + word.length() != str.length()) {
newstr += str.charAt(index - 1);
newstr += str.charAt(index + word.length());
deender += i + 1;
} else {
newstr += str.charAt(index - 1);
deender += i + 1;
}
} else {
deender += i;
}
}
return newstr;
}
public String wordEnds(String str, String word) {
ReplyDeleteStringBuilder finalOutput = new StringBuilder();
String windowTemporaryString = "";
for (int i = 0; i <= str.length() - word.length() - 1; i++) {
windowTemporaryString = str.substring(i, i + word.length() + 1);
boolean targetWordFoundAtRight = windowTemporaryString.substring(1, word.length() + 1).equals(word);
boolean targetWordFoundAtLeft = windowTemporaryString.substring(0, word.length()).equals(word);
if (targetWordFoundAtRight) {
char storeFarLeftCharOnWindowTemporaryString = windowTemporaryString.charAt(0);
finalOutput.append(storeFarLeftCharOnWindowTemporaryString);
}
if (targetWordFoundAtLeft) {
String storeFarRightCharOnWindowTemporaryString = windowTemporaryString.substring(word.length(), word.length() + 1);
finalOutput.append(storeFarRightCharOnWindowTemporaryString);
}
}
return finalOutput.toString();
}