Skip to content

Instantly share code, notes, and snippets.

View volkanbicer's full-sized avatar
🔥

Volkan Bicer volkanbicer

🔥
View GitHub Profile
@volkanbicer
volkanbicer / maxSumOfSubArray.swift
Created November 12, 2017 11:30
Maximum sum of sub array with Kadane's algorithm
func maxSumOfSubArray(_ a:[Int]) -> Int?{
guard a.count > 0 else { return nil }
var maxCurrent = a[0]
var maxGlobal = a[0]
for i in a.dropFirst(){
maxCurrent = max(i, maxCurrent + i)
if maxCurrent > maxGlobal{
maxGlobal = maxCurrent
}
}
func uniquePaths(_ m: Int, _ n: Int) -> Int {
var paths = [[Int]](repeating: [Int](repeating: 0, count: n+1), count: m+1)
return helper(m, n, &paths)
}
func helper(_ row: Int, _ col: Int, _ paths: inout [[Int]]) -> Int{
if row == 1 || col == 1{ return 1 }
if paths[row][col] == 0{
paths[row][col] = helper(row, col-1, &paths) + helper(row-1, col, &paths)
}
@volkanbicer
volkanbicer / lca.swift
Created November 11, 2017 21:32
Lowest common ancestor
func lca(_ root: TreeNode<Int>, _ x: Int, _ y: Int) -> TreeNode<Int>?{
guard let pathToX = pathTo(root, x), let pathToY = pathTo(root, y) else { return nil}
var current: TreeNode? = nil
while !pathToX.isEmpty && !pathToY.isEmpty{
let a = pathToX.pop()
let b = pathToY.pop()
if a == b{
current = a
}else{
break
@volkanbicer
volkanbicer / longestConsecutive.swift
Created November 11, 2017 21:09
Longest consecutive charter in string
func longestConsecutive(_ str: String) -> [String: Int]{
var maxCount = 1
var maxChar = str[str.startIndex]
var count = 1
var previousChar = str[str.startIndex]
for char in str.dropFirst(){
if char == previousChar{
count += 1
@volkanbicer
volkanbicer / mergeSort.swift
Created November 11, 2017 17:21
Merge sort implementation in swift.
//Top down implementation
func mergeSort(_ a: [Int]) -> [Int]{
guard a.count > 1 else { return a }
let mid = a.count/2
let leftArray = mergeSort(Array(a[0..<mid]))
let rightArray = mergeSort(Array(a[mid..<a.count]))
return merge(leftArray, rightArray)
}
func countOccurence(_ key: Int, _ a: [Int]) -> Int{
func leftBoundry() -> Int{
var low = 0
var high = a.count
while low<high{
let mid = low + (high - low) / 2
if a[mid] < key{
low = mid + 1
}else{
high = mid
@volkanbicer
volkanbicer / booyerMoore.swift
Created November 11, 2017 15:41
Swift string search algorithm
func search(_ str: String, _ pattern: String) -> String.Index?{
if pattern.characters.count > str.characters.count{
return nil
}
var skipTable = [Character: Int]()
let n = pattern.characters.count
for (i, char) in pattern.characters.enumerated(){
skipTable[char] = n - 1 - i
}
@volkanbicer
volkanbicer / bruteForceStringSearch.swift
Created November 11, 2017 15:17
Brute force string search.
func search(_ str: String, _ pattern: String) -> Int{
if pattern.characters.count > str.characters.count{
return -1
}
for (i,index) in str.characters.indices.enumerated(){
var currentIndex = index
var found = true
for char in pattern.characters{
if currentIndex == str.endIndex || char != str[currentIndex]{
found = false
@volkanbicer
volkanbicer / minmax.swift
Created November 11, 2017 15:06
Minumum and maximum in array
func minmax(_ a: [Int]) -> (Int,Int)?{
if a.count < 1 {
return nil
}
var minInArray = a[0]
var maxInArray = a[0]
for number in a{
if number < minInArray{
minInArray = number
}
@volkanbicer
volkanbicer / longestCommonSubsequence.swift
Created November 11, 2017 12:56
Longetset common subsequence string extension
extension String{
public func longestCommonSubsequence(_ other: String) -> String{
func lcsLength(_ other: String) -> [[Int]]{
let n = characters.count
let m = other.characters.count
var matrix = [[Int]](repeating:[Int](repeating: 0, count: m+1), count:n+1)
for (i, char) in characters.enumerated(){
for (j, otherChar) in other.characters.enumerated(){
if char == otherChar{
// Common char found, add 1 to highest lcs found so far.