title: How to return number of distinc string repeatance separated by white Space in String?
body: <p>Given a StringX with characters repeated in a fashion of which it is not tied to each other, this method returns the distinct number of times such string of character is present in StringX.
  E.g String y = "the green thermometer in the room" has "the" repeated in three places, THE green THErmometer in THE room, but only the first and last are distinct separated by white space. the method ignores the second occurrence of THE and returns "2"
  using a bruteforce approach.</p>
  
  <p>The code has a bug I can't fix since I am learning java if the first letter of the string argument is indented with a space,like  " example" instead of "example" it gives a result I cant even tell how it came about with. 
  A simplified efficient approach will be much appreciated.</p>
  
  <pre><code>    class Test{
      public Test(){
      }
  
      private int numberOfDistinctOccurence(String string, String token) {
          int tokLength = token.length();
          boolean lastEndsWithSpace, previousIsSpace, nextIsSpace, isFirstSentence;
          boolean isEqual = lastEndsWithSpace = previousIsSpace = nextIsSpace = isFirstSentence = false;
          int count = 0;
          for (int shift = 0, stopCount = 0; stopCount &lt; string.length() - token.length(); 
                                                      stopCount++, shift++, tokLength++) {
              isEqual = (string.substring(shift, tokLength).equalsIgnoreCase(token));
              lastEndsWithSpace = (string.substring(string.length()).equals(" ") || 
                                      (string.substring(string.length()).equals("")));
              if (shift == 1) {
                  previousIsSpace = (string.substring(shift - 1, shift).equals(" "));
              }
              nextIsSpace = (string.substring(tokLength, tokLength + 1).equals(" "));
              isFirstSentence = (shift == 0 &amp;&amp; string.substring(0, 0).equals("") || nextIsSpace);
              if (isEqual &amp;&amp; isFirstSentence) {
                  count++;
              } else if (isEqual &amp;&amp; nextIsSpace || lastEndsWithSpace &amp;&amp; previousIsSpace) {
                  count++;
              }
          }
          int x = string.lastIndexOf(token.substring(token.length())); // index of last tokens char
          if (string.substring(x - token.length(), x).equalsIgnoreCase(token)) {
              if (string.length() == token.length() &amp;&amp; string.equalsIgnoreCase(token)) {
              } else {
                  count = (string.substring(x - token.length() - 1, x - token.length()).
                                              equalsIgnoreCase(" ")) ? count + 1 : count;
              }
          }
          return count = string.length() == token.length() &amp;&amp; string.equalsIgnoreCase(token) ? 1 : count;
  
  }
      public static void main(String[] args) {
          Test test = new Test();
          System.out.println(test.numberOfDistinctOccurence("The green Thermometer in the house", "he"));
      }
  }
  </code></pre>