Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78371b3

Browse files
Longest Substring solution with bit older class refactoring
1 parent d6f6181 commit 78371b3

File tree

5 files changed

+87
-35
lines changed

5 files changed

+87
-35
lines changed

‎.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ hs_err_pid*
2424
replay_pid*
2525

2626
# Kotlin Gradle plugin data, see https://kotlinlang.org/docs/whatsnew20.html#new-directory-for-kotlin-data-in-gradle-projects
27-
.kotlin/
27+
.kotlin/
28+
.idea/
29+
out/

‎src/main/kotlin/Main.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package main.kotlin
22

3-
import TwoSum
3+
import main.kotlin.dsalgoleetcode.LongestSubstring
4+
import main.kotlin.dsalgoleetcode.TwoSum
45

56
fun main() {
67
println("What's your name?")
78
val name = readln()
89
println("Hello, $name!")
910

1011
// Calling Two Sum class
11-
val obj = TwoSum()
12-
val result = obj.twoSum(nums = intArrayOf(1,2,3,4,5), target = 9)
12+
val twoSum = TwoSum()
13+
val result = twoSum.twoSum(nums = intArrayOf(1, 2, 3, 4, 5), target = 9)
14+
15+
// Calling Longest Substring
16+
val longestSubstring = LongestSubstring()
17+
val lsResult = longestSubstring.lengthOfLongestSubstring(s = "abcabcbbb")
1318

1419
println("Indices: ${result.joinToString(",")}")
20+
println("Longest Substring : $lsResult")
1521
}

‎src/main/kotlin/ds-algo-leetcode/TwoSum.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* Time Complexity O(n)
5+
* Longest Substring Without Repeating Characters
6+
* Given a string s, find the length of the longest substring without duplicate characters.
7+
*
8+
* Example 1:
9+
*
10+
* Input: s = "abcabcbb"
11+
* Output: 3
12+
* Explanation: The answer is "abc", with the length of 3.
13+
* Example 2:
14+
*
15+
* Input: s = "bbbbb"
16+
* Output: 1
17+
* Explanation: The answer is "b", with the length of 1.
18+
* Example 3:
19+
*
20+
* Input: s = "pwwkew"
21+
* Output: 3
22+
* Explanation: The answer is "wke", with the length of 3.
23+
* Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
24+
*/
25+
26+
class LongestSubstring {
27+
28+
fun lengthOfLongestSubstring(s: String): Int {
29+
val seen = mutableMapOf<Char,Int>()
30+
var start = 0
31+
var maxLength = 0
32+
33+
for((index, element) in s.withIndex()){
34+
if( element in seen && seen[element]!! >= start){
35+
start = seen[element]!! + 1
36+
}
37+
seen[element] = index
38+
maxLength = maxOf(maxLength, index - start + 1)
39+
}
40+
return maxLength
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* Time Complexity O(n)
5+
* Space Complexity O(n)
6+
*
7+
* Step 1 : Declare a Map of Integers which will be used for comparison
8+
* Step 2 : Run through the array with index
9+
* Step 3 : Subtract the current element from target sum and store it
10+
* Step 4 : Check if the complement present in the map
11+
* Step 5 : If present then create and return an array of Integers with the index
12+
* of the current element and fetched value from map with the complement key
13+
* Step 6 : Otherwise set the map with current element as key and current index
14+
* as value
15+
* Step 7 : Return an empty array by default
16+
*/
17+
18+
class TwoSum {
19+
20+
fun twoSum(nums: IntArray, target: Int): IntArray {
21+
val seen = mutableMapOf<Int, Int>()
22+
23+
// using array with index as index and value both are required for computation
24+
for((index, element) in nums.withIndex()){
25+
val complement = target - element
26+
if( complement in seen )
27+
return intArrayOf(seen[complement]!!, index)
28+
// setting the map with the current index
29+
seen[element] = index
30+
}
31+
return intArrayOf()
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /