We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6271ccb commit d7152a3Copy full SHA for d7152a3
longest-substring-without-repeating-characters/yhkee0404.scala
@@ -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