diff --git a/climbing-stairs/tigermint.kt b/climbing-stairs/tigermint.kt new file mode 100644 index 0000000000..6721569d5c --- /dev/null +++ b/climbing-stairs/tigermint.kt @@ -0,0 +1,16 @@ +class Solution { + fun climbStairs(n: Int): Int { + if (n == 1) return 1 + if (n == 2) return 2 + + val dp = IntArray(n + 1) { 0 } + dp[1] = 1 + dp[2] = 2 + + for (i in 3 .. n) { + dp[i] = dp[i - 1] + dp[i - 2] + } + + return dp[n] + } +} diff --git a/product-of-array-except-self/tigermint.kt b/product-of-array-except-self/tigermint.kt new file mode 100644 index 0000000000..21a4e0c4b2 --- /dev/null +++ b/product-of-array-except-self/tigermint.kt @@ -0,0 +1,34 @@ +// class Solution { +// fun productExceptSelf(nums: IntArray): IntArray { +// val prefixProduct = IntArray(nums.size) { _ -> 1 } +// val suffixProduct = IntArray(nums.size) { _ -> 1 } + +// for (i in 1 until nums.size) { +// prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1] +// } + +// for (i in nums.size - 2 downTo 0) { +// suffixProduct[i] = suffixProduct[i + 1] * nums[i + 1] +// } + +// return IntArray(nums.size) { prefixProduct[it] * suffixProduct[it] } +// } +// } + +class Solution { + fun productExceptSelf(nums: IntArray): IntArray { + val answer = IntArray(nums.size) { _ -> 1 } + + for (i in 1 until nums.size) { + answer[i] = answer[i - 1] * nums[i - 1] + } + + var right = 1 + for (i in nums.size - 1 downTo 0) { + answer[i] *= right + right *= nums[i] + } + + return answer + } +} diff --git a/valid-anagram/tigermint.kt b/valid-anagram/tigermint.kt new file mode 100644 index 0000000000..472912c309 --- /dev/null +++ b/valid-anagram/tigermint.kt @@ -0,0 +1,20 @@ +class Solution { + fun isAnagram(s: String, t: String): Boolean { + val sCharToCount = mutableMapOf() + val tCharToCount = mutableMapOf() + + s.forEach { sCharToCount[it] = (sCharToCount[it] ?: 0) + 1 } + t.forEach { tCharToCount[it] = (tCharToCount[it] ?: 0) + 1 } + + val sKeys: MutableSet = sCharToCount.keys + val tKeys: MutableSet = tCharToCount.keys + + if (sKeys != tKeys) return false + sKeys.forEach { + if (sCharToCount[it] != tCharToCount[it]) { + return false + } + } + return true + } +}