Thursday, April 30, 2015

Thursday, April 30th

Today was a regular class. I went up to the Genius Bar with Tommy, but unfortunately there were no customers.

I worked as usual on string 2. Today was a particularly good working day for me. I did one problem that I got on (almost) the first try, and I was the happiest person on earth. I was also happy because it proved that I was understanding the logic. I got to half of String 2 today. Only half of the problems left !

Today I used once again the Java string oracle documentation. It is SUPER useful. I learned a method, that will most likely help me out greatly in the future. It is the str.contain() method.

In one of the problems, I tried to use another method called math.Max(), but CodingBat didn't let me use it. It didn't let me, because it was a static int method, and for some reason CodingBat doesn't like that.

Apart from that, I don't really have anything special to say. This is today's problem:
----------------------------------------------------------------------------------------------------------------------------------

Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().

prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false


public boolean prefixAgain(String str, int n) {

 
  for (int i =0;i<str.length()-n;i++)
  {
 
  if (str.substring(n).contains(str.substring(0,n)))
  {
  return true;
// if the string contains the prefix after the first time that it appears, return true;
 
  }
 
  }
 
  return false;
// if the prefix does not reappear, then return false;
}

Looking forward to continuing with strings.

No comments:

Post a Comment