Improved the lengthOfRepetition Function