I was solving this problem https://leetcode.com/problems/longest-substring-without-repeating-characters/
I submitted a kotlin solution which had runtime of 190ms and then I just converted this code to java code and the runtime was 2ms. Why is this the case?
Following is my kotlin code
class Solution {
fun lengthOfLongestSubstring(s:String):Int {
val exists = BooleanArray(256)
var i = 0
var j = 0
var ans = 0
while (j < s.length) {
if (!exists[s.get(j).toInt()]) {
exists[s.get(j).toInt()] = true
j++
ans = Math.max(ans, j - i)
} else {
exists[s.get(i).toInt()] = false
i++
}
}
return ans
}
}And following is my java code
class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] exists = new boolean[256];
int i = 0, j = 0, ans = 0;
while (j < s.length()) {
if (!exists[(int) s.charAt(j)]) {
exists[(int) s.charAt(j)] = true;
j++;
ans = Math.max(ans, j - i);
} else {
exists[(int) s.charAt(i)] = false;
i++;
}
}
return ans;
}
}