             let ch = String(Array(first)[i])
             
             // See if the character is within the limit positions away from the original position of that character.
-            for j in max(0, i - limit) ..< min(i + limit, count(second)) {
-                
-                if String(Array(copy)[j]) == ch {
-                    common = common + ch
-                    let nsRange : NSRange = NSRange(location: j, length: 1)
-                    let copy = (copy as NSString).stringByReplacingCharactersInRange(nsRange, withString: "*")
-                    break
+            
+            let jStart = max(0, i - limit)
+            let jEnd = min(i + limit, count(second))
+            
+            if jStart < jEnd {
+                for j in jStart ..< jEnd {
+                    
+                    if String(Array(copy)[j]) == ch {
+                        common = common + ch
+                        let nsRange : NSRange = NSRange(location: j, length: 1)
+                        let copy = (copy as NSString).stringByReplacingCharactersInRange(nsRange, withString: "*")
+                        break
+                    }
                 }
             }
         }

        // Limit the result to 4.
        return result > 4 ? 4 : result
    }

    func getCommonPrefix(first: String, second: String) -> String {
        
        if first == "" {

println("distance : \(d)")   // distance: 0.9611


let jaro1 = JaroDistance(firstString: "Hello", lastString: "hey")
let d1 = jaro1.getJaroWinklerDistance()
println("distance : \(d1)") // distance: 0.688


