Skip to content

Commit d7152a3

Browse files
authored
longest substring without repeating characters solution
1 parent 6271ccb commit d7152a3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.util.control.Breaks._
2+
3+
object Solution {
4+
def lengthOfLongestSubstring(s: String): Int = {
5+
var ans = 0
6+
val visited = Array.fill(128)(false) // S(n) = O(1)
7+
var i = 0
8+
var j = 0
9+
while (j != s.length) {
10+
breakable { // but no continue in scala!
11+
while (j != s.length) { // T(n) = O(n)
12+
if (visited(s(j))) {
13+
break
14+
}
15+
visited(s(j)) = true
16+
j += 1
17+
}
18+
}
19+
ans = Math.max(ans, j - i)
20+
21+
visited(s(i)) = false
22+
i += 1
23+
}
24+
ans
25+
}
26+
}

0 commit comments

Comments
 (0)