Need Help with Writing methods that involves Loops
Hello Forum, I am a new user and I have been at this problem for over 2 hours with my friends. We honestly don't know what's wrong and if could help us that would be awesome :D
The question is,
Given a string, return the longest substring that appears at both the beginning and end of the string without overlapping. For example, sameEnds("abXYab") is "ab".
Example are:
sameEnds("abXYab") -> "ab"
sameEnds("xx") -> "x"
sameEnd("x") -> "x"
What we have tried so far is:
Code :
public static String sameEnds(String str)
{
int len = str.length();
String output = "";
boolean b = true;
if(len == 0 || len == 1)
return str;
if(len == 2 && str.charAt(0) == str.charAt(1))
return str.charAt(0) +"";
if(len == 3 && str.charAt(0) == str.charAt(2))
return str.charAt(0) +"";
for(int i = 0; i < len / 2; i++)
{
if(str.substring(i,i+1).equals(str.substring(len-(i+1),(len-i))) && b)
output += str.substring(i,i+1);
else
b = false;
}
return output;
}
If anyone could help us it would be great.
Also, another problem I am having is with this question:
The Evil E is back and it brought back some friends! Write a method that will acknowledge that the letter E is evil. Each letter E will eliminate that characters around it. But the Evil E's will not eliminate its friends. So it will no eliminate the letters e or E.
Example are:
evilEs("HELLO") -> " E LO"
evilEs("dEep") -> " Eep"
evilEs("Evil") -> "E il"
evilEs("bean") -> "bean"
evilEs("AbEEmgmEeodE") -> "A EE g Eeo E"
evilEs("ELEPHANT") -> "E E HANT"
evilEs("HomepagE") -> "Homepa E"
I haven't really tried anything for this one but any help is good help.
Thank you for reading this :)
Re: Need Help with Writing methods that involves Loops
I wrote a quick program using your method, and here are the results:
racecar --> rac
races race -->
corn -->
sisters --> s
bordiogbo -->
So, only sisters and corn worked correctly. Racecar should have returned r, races race should have returned race, and bordiogbo should have returned bo.
So, your program picks up palidromic patterns (backwards = forwards), but I don't think that's what you want. Your program starts at the ends of the String and works inward, but you want the letter patterns to occur in the same order (as I understand it). I think the best way to do this is to start with your substring comparisons at half the String's length, then work down instead of starting your for-loop at 0. My reasoning for this is so: if you start with a small substring, your program may notice that the entire String both starts and ends with "s". However, it might then overlook the fact that the String also starts and ends with "sas". By starting with the largest possible common substring, you eliminate the possibility of overlooking matching substrings.
Also, note that your substrings will not just be one character long. As in my example of "races race," you can see that the common ending is actually four characters long. However, you program only assumes common endings of length one because it uses this code:
str.substring(len-(i+1),(len-i))
str.substring(i,i+1)
This is difficult to get my ideas across, but hopefully this helps some.
Now, to your next problem...
Think about what your program needs to do: it needs to check each letter of a String to see if it is an "E," and then remove characters from the String in indexes that are one less and one greater than the indexes of found E's (unless! the characters in those positions are E's or e's). I'd start simple: write a program that can check each character of a String.
Re: Need Help with Writing methods that involves Loops
I finished the first problem and ended up getting:
Code :
public static String sameEnds(String str)
{
int len = str.length();
String output = "";
if(len==0)
return "";
if(len==1)
return str;
if(len == 2 && str.charAt(0)== str.charAt(1))
return str.charAt(0) +"";
if(len == 3 && str.charAt(0)== str.charAt(2))
return str.charAt(0) +"";
for(int i = 0; i < len / 2; i++)
{
if(str.substring(0,len/2-i).equals(str.substring(len/2+i+1,len)) &&len %2 ==1)
return str.substring(0,len/2-i);
if(str.substring(0,len/2-i).equals(str.substring(len/2+i,len)))
return str.substring(0,len/2-i);
}
return output;
}
as for the second one, i have this so far:
Code :
public static String evilEs(String str)
{
String output = "";
if(str.indexOf('E') == -1)
return str;
else
{
for(int i = 0; i < str.length(); i++)
{
String temp = "X" + str + "X";
if(temp.charAt(i) == 'E' && (temp.charAt(i - 1) != 'E' || temp.charAt(i - 1) != 'e') && (temp.charAt(i + 1) != 'E' || temp.charAt(i + 1) != 'e'))
}
}
return output;
}
and the problem im having is subtracting the index before and after i from the string.
Re: Need Help with Writing methods that involves Loops
Quote:
I finished the first problem
Does it work?
Quote:
String temp = "X" + str + "X";
Why are you doing this?
Quote:
the problem im having is subtracting the index before and after i from the string.
Okay. Think about what you are actually doing here: instead of thinking of this as removing the characters, think about it as taking every character BUT the ones next to E's. So, you need to take substrings from before and in between the characters you want to remove.
To visualize what I'm saying, think of this example:
evilE's("crEate")
crEate
The black sections are the parts of the String you want to keep, and you can do this by adding substrings together.
Re: Need Help with Writing methods that involves Loops
I am using the temp to avoid the out-of-bounds error for when E is the first letter of a String.
Yes the first problem works.
Re: Need Help with Writing methods that involves Loops
Quote:
I am using the temp to avoid the out-of-bounds error for when E is the first letter of a String.
Okay. There is a small problem with this: if there is an E as the first character of the String, the X will get removed, and then when you try to get rid of the character where you THINK the X is, it's actually the E, and then you have loads of problems... Instead, I'd just use if statements to account for the ends of the String (ie, if(i == str.length() - 1) or if(i == 0), then have the program react accordingly).
if(temp.charAt(i) == 'E' && (temp.charAt(i - 1) != 'E' || temp.charAt(i - 1) != 'e') && (temp.charAt(i + 1) != 'E' || temp.charAt(i + 1) != 'e'))
I'd also revise this; what if the character before an E is g and after is e? This statement overlooks that possibility by requiring BOTH of the characters surrounding the E to not be E or e. Instead, I would first check if the current character is E, and then put separate if statements within the first if statement to check the surrounding characters one at a time.
Re: Need Help with Writing methods that involves Loops
Hi,
The aproximmation is like palindrome words. If you search palindrome program, you can find a lot.
Oscar Gomez
Engineer at PSL
PSL S.A. - CMMi 5 nearshore software development and outsourcing from Latin America - Home
Re: Need Help with Writing methods that involves Loops
and the problem im having is subtracting the index before and after i from the string.
I agree with the group, to read 3 characters from your string at a time, then store them in past, current, and next
it is true that you would like to know what past is, but this is handled as you iterate through your string.
special cases: for the first character you will have no past, and for the last character you will have no next.
you are working with current. so for your first character you write a special case for it before you jump into your loop.
when you are running your loop start by only caring about what current wants. check it for being an E then do accordingly,
move all your variables over, and store past every time. since past has been checked for Es on both sides it is safe to add!
then update next by collecting it from your string. repeat the loop until you meet the end condition and write code to handle the second special case.
goodluck,
Jonathan.