Saturday, June 4, 2016

Future training - StrSymmetryPoint

Here is another codility problem solution from the codility lessons (StrSymmetryPoint - Find a symmetry point of a string, if any.) due to the copy rights I can't copy the content of the problem here so to view the problem description click here.




// you can also use imports, for example:
// import java.util.*;

// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
 public int solution(String S) {

  // write your code in Java SE 8
  if (S.length() == 1) return 0;
  else if (S.length() == 0 || S.length() % 2 == 0) return -1;
  else {
   int len = S.length();
   int half = (len / 2);
   for (int i = 0; i < half; i++) {
    if (S.charAt(i) != S.charAt((len - 1) - i)) return -1;
   }
   return (len / 2);
  }
 }
}

No comments:

Post a Comment