Advertisement
ksoltan

FibonacciString

May 7th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. public class FibonacciString {
  2.     /**
  3.      * Write a program in Java to assess a given string whether it complies with
  4.      * following patterns. Return true if a given string complies with these
  5.      * patterns else false.
  6.      *
  7.      * N = N1 + N2 N >= N1 >= N2
  8.      *
  9.      * where N is the Nth element in the string or element at Nth position; N1
  10.      * is the (N-1) element in the string & N2 is the (N-2) element in the
  11.      * string.
  12.      *
  13.      * Example 1: 224610 Elements in this string are 2, 2, 4, 6, 10. First Set:
  14.      * 2+2=4 (N2=2; N1=2 & N= 4); Second Set: 2+4=6 (N2=2; N1=4 & N=6); Third
  15.      * Set: 4+6=10 (N2=4; N1=6 & N= 10)
  16.      *
  17.      * Example 2: 1112233558 Elements in this string are 1, 11, 12, 23, 35, 58
  18.      *
  19.      * Example 3: 1101102203 Elements in this string are 1, 101, 102, 203.
  20.      *
  21.      * @param str
  22.      * @return
  23.      */
  24.     public static boolean isFibonacciString(String str) {
  25.         // To be a fib string, the string must have at least three elements.
  26.         // Start searching for the two start elements by taking the first two
  27.         // characters of the string.
  28.         return str.length() >= 3
  29.                 && getSetups(Integer.parseInt(str.charAt(0) + ""),
  30.                         Integer.parseInt(str.charAt(1) + ""), str.substring(2));
  31.     }
  32.  
  33.     static boolean getSetups(int n2, int n1, String str) {
  34.         // if n2 is greater than or equal to n1 and there are no more elements
  35.         // to try, this is not a fib string
  36.         if (str.length() < 1 && n2 >= n1) {
  37.             return false;
  38.         }
  39.         // check if the current set up is a fib string. Otherwise, check whether
  40.         // appending the next element in string to n1 will work. Otherwise,
  41.         // check whether incrementing n2 and resetting n1 will work
  42.         return (str.length() > 0 && n1 >= n2 && isSetupFib(n2, n1, str))
  43.                 || (str.length() > 0 && getSetups(n2,
  44.                         Integer.parseInt(n1 + "" + str.charAt(0)),
  45.                         str.substring(1)))
  46.                 || (str.length() < 1 && getSetups(
  47.                         Integer.parseInt("" + n2 + ("" + n1).charAt(0)),
  48.                         Integer.parseInt(("" + n1).substring(1, 2)),
  49.                         ("" + n1).substring(2)));
  50.     }
  51.  
  52.     static boolean isSetupFib(int n2, int n1, String str) {
  53.         // the sequence has ended perfectly, there are no straggling numbers
  54.         if (str.length() < 1) {
  55.             return true;
  56.         }
  57.         int n = n2 + n1;
  58.         String nStr = n + "";
  59.         // check if the next term in the string is what it should be, elem(n-2)
  60.         // + elem(n-1). If it is not, the string is automatically not a fib
  61.         // string.
  62.         if (str.length() < nStr.length()
  63.                 || !str.substring(0, nStr.length()).equals(nStr)) {
  64.             return false;
  65.         }
  66.         // if so far the elements are correct, check the next term
  67.         return isSetupFib(n1, n, str.substring(nStr.length()));
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement