├── .DS_Store ├── Algorithms ├── .DS_Store ├── Search │ ├── BinarySearch │ └── LinearSearch.playground │ │ ├── Contents.swift │ │ ├── contents.xcplayground │ │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ └── user.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── Sort │ ├── BubbleSort │ ├── BubbleSort.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ └── user.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── user.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist │ ├── InsertionSort.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ └── user.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── user.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist │ ├── MergeSort.playground │ ├── Contents.swift │ └── contents.xcplayground │ ├── SelectionSort.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ └── user.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── user.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist │ └── SelectionSortAlt.playground │ ├── Contents.swift │ └── contents.xcplayground ├── DataStructure ├── LinkedList │ ├── DoublyLinkedList │ │ ├── DoublyLinkedList.swift │ │ └── Link.swift │ └── SinglyLinkedList │ │ ├── DoubleEndedLinkedList.swift │ │ ├── Link.swift │ │ ├── LinkQueue.swift │ │ ├── LinkStack.swift │ │ └── LinkedList.swift ├── QueueArray.swift ├── QueueUsingTwoStacks.swift ├── StackArray.swift └── Tree │ └── BinaryTree │ └── BinaryTree.swift ├── LICENSE ├── ProblemSolving ├── AddTwoLinkList.swift ├── AddingArrayDigit.swift ├── Anargram.swift ├── ArrayRotate.swift ├── ArraySubset ├── BurstBalloons ├── BuySellStock ├── BuySellStock2 ├── BuySellStock3 ├── CapacityToShipPackagesWithinDDays ├── CommonCharacter ├── CountPair ├── CountSpace.swift ├── Fibonacci.swift ├── FindDuplicate ├── FindGreatestCommonDivisorofArray ├── FindIntegers.swift ├── FindTheMissingNumber.swift ├── FizzBuzz ├── GCD.swift ├── ImplementQueueUsingStacks ├── InsertElementAfterEveryNthCharacter ├── IntegerPalindrome.swift ├── Intersection.swift ├── KClosestPointsToOrigin ├── KthLargestElementInAnArray ├── LinkedListCycle ├── LongestCommonPrefix.swift ├── LongestSub.swift ├── LongestSubstringWithoutRepeatingCharacters ├── LowestCommonAncestorOfABinarySearchTree ├── LowestCommonAncestorOfABinaryTree ├── MaxSecondMax ├── MergeTwoSortedList ├── MinimumDifference ├── MoveZeroes ├── NearestDistanceToCharacter ├── Palindrome.swift ├── PermutationInString ├── PlusOne ├── PrimeNumber.swift ├── PrintPatterns.swift ├── RemoveDuplicateSortedArray ├── RemoveDuplicates.swift ├── ReversWordsInString ├── ReverseArrayOfString ├── ReverseInteger.swift ├── ReverseLinkedList ├── ReverseWord.swift ├── RomanToInteger.swift ├── RotateImage ├── SearchInsertPosition.swift ├── SumOfAllDigits.swift ├── SwapWithoutTemp.swift ├── TopKFrequentElements ├── TwoSum ├── TwoSumSortedArray ├── ValidPalindrome └── ValidParentheses.swift └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/.DS_Store -------------------------------------------------------------------------------- /Algorithms/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/Algorithms/.DS_Store -------------------------------------------------------------------------------- /Algorithms/Search/BinarySearch: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-704: https://leetcode.com/problems/binary-search/description/ 2 | // Example Input: nums = [-1,0,3,5,9,12], target = 9 3 | // Output: 4 4 | 5 | class Solution { 6 | func search(_ nums: [Int], _ target: Int) -> Int { 7 | var low = 0 8 | var high = nums.count - 1 9 | 10 | while low <= high { 11 | let mid = (high + low) / 2 12 | if nums[mid] == target { 13 | return mid 14 | } 15 | if nums[mid] < target { 16 | low = mid + 1 17 | } else if nums[mid] > target { 18 | high = mid - 1 19 | } 20 | } 21 | 22 | return -1 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Algorithms/Search/LinearSearch.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //Linear Search 2 | //by Sayed Mahmudul Alam 3 | 4 | func linearSearch(key: Int, array: [Int]) -> Int { 5 | for i in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /Algorithms/Search/LinearSearch.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Algorithms/Search/LinearSearch.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/Algorithms/Search/LinearSearch.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | var input = [1,5,3,7,2,9,5,4,8,6,1] 4 | 5 | func bubbleSort(_ input: inout [Int]) -> [Int] { 6 | var k = 0 7 | 8 | for _ in 0.. input[j + 1] { 11 | let temp = input[j] 12 | input[j] = input[j + 1] 13 | input[j + 1] = temp 14 | } 15 | } 16 | k += 1 17 | } 18 | return input 19 | } 20 | 21 | print(bubbleSort(&input)) 22 | -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Bubble Sort 2 | // Sayed Mahmudul Alam 3 | 4 | func swap(left: Int, with right: Int, array: inout [Int]) { 5 | let temp = array[left] 6 | array[left] = array[right] 7 | array[right] = temp 8 | } 9 | 10 | func bubbleSort(array: inout [Int]) -> [Int] { 11 | for i in (0.. array[j + 1]) { 14 | swap(left: j, with: j + 1, array: &array) 15 | } 16 | } 17 | } 18 | return array 19 | } 20 | 21 | var numbers = [4,8,5,1,7] 22 | print(bubbleSort(array: &numbers)) 23 | -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/Algorithms/Sort/BubbleSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Algorithms/Sort/BubbleSort.playground/xcuserdata/user.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | BubbleSort (Playground).xcscheme 8 | 9 | isShown 10 | 11 | orderHint 12 | 0 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Algorithms/Sort/InsertionSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Insertion Sort 2 | // Sayed Mahmudul Alam 3 | 4 | func insertionSort(array: inout [Int]) { 5 | 6 | var inn = 0 7 | 8 | for out in 1..= 0 && array[inn] > temp) { 13 | array[inn + 1] = array[inn] 14 | inn -= 1 15 | } 16 | array[inn + 1] = temp 17 | } 18 | } 19 | 20 | var numbers = [4,8,5,10,12] 21 | print(insertionSort(array: &numbers)) 22 | -------------------------------------------------------------------------------- /Algorithms/Sort/InsertionSort.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Algorithms/Sort/InsertionSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Algorithms/Sort/InsertionSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/Algorithms/Sort/InsertionSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Algorithms/Sort/InsertionSort.playground/xcuserdata/user.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | InsertionSort (Playground).xcscheme 8 | 9 | isShown 10 | 11 | orderHint 12 | 0 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Algorithms/Sort/MergeSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Merge Sort 2 | // Sayed Mahmudul Alam 3 | 4 | struct MergeSort{ 5 | 6 | var size: Int? 7 | var temp: Array 8 | var array: Array 9 | 10 | init(size: Int, array: Array) { 11 | self.size = size 12 | self.array = array 13 | temp = Array(repeating: 0, count: size) 14 | } 15 | 16 | mutating func sort() -> Array { 17 | mergeSort(low: 0, high: size! - 1) 18 | return array 19 | } 20 | 21 | mutating func mergeSort(low: Int, high: Int) { 22 | if low < high { 23 | 24 | let middle = low + (high - low) / 2 25 | mergeSort(low: low,high: middle) 26 | mergeSort(low: middle + 1, high: high) 27 | merge(low: low, middle: middle, high: high) 28 | } 29 | } 30 | 31 | mutating func merge(low: Int, middle: Int, high: Int) { 32 | 33 | for i in low...high { 34 | temp[i] = array[i] 35 | } 36 | 37 | var i = low 38 | var j = middle + 1 39 | var k = low 40 | 41 | while(i <= middle && j <= high) { 42 | 43 | if temp[i] <= temp[j] { 44 | array[k] = temp[i] 45 | i += 1 46 | } else { 47 | array[k] = temp[j] 48 | j += 1 49 | } 50 | 51 | k += 1 52 | } 53 | 54 | while(i <= middle) { 55 | array[k] = temp[i] 56 | k += 1 57 | i += 1 58 | } 59 | } 60 | } 61 | 62 | var mergeSort = MergeSort(size: 5, array: [5,8,10,23,7]) 63 | print(mergeSort.sort()) 64 | -------------------------------------------------------------------------------- /Algorithms/Sort/MergeSort.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Selection sort 2 | // Sayed Mahmudul Alam 3 | 4 | private func swap(left: inout Int, with right: inout Int, array: inout [Int]) { 5 | let temp = array[left] 6 | array[left] = array[right] 7 | array[right] = temp 8 | } 9 | 10 | func selectionSort(array: inout [Int]) { 11 | var min = 0 12 | 13 | for var out in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/265fd550d3cfac7ef8c6c9025a2709f4eb855c85/Algorithms/Sort/SelectionSort.playground/playground.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSort.playground/xcuserdata/user.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SelectionSort (Playground).xcscheme 8 | 9 | isShown 10 | 11 | orderHint 12 | 0 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSortAlt.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Selection sort 2 | // Sayed Mahmudul Alam 3 | 4 | import Foundation 5 | 6 | struct SelectionSortAlt { 7 | 8 | private func findSmallest(array: [Int]) -> Int { 9 | var smallest = array[0] 10 | var smallestIndex = 0 11 | 12 | for i in (0.. [Int] { 22 | var sorted = [Int]() 23 | for i in array { 24 | let smallestIndex = findSmallest(array: array) 25 | sorted.append(array[smallestIndex]) 26 | array.remove(at: smallestIndex) 27 | } 28 | return sorted 29 | } 30 | } 31 | 32 | var selectionSort = SelectionSortAlt() 33 | var numbers = [5,14,7,2,4] 34 | print(selectionSort.sort(array: &numbers)) 35 | -------------------------------------------------------------------------------- /Algorithms/Sort/SelectionSortAlt.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /DataStructure/LinkedList/DoublyLinkedList/DoublyLinkedList.swift: -------------------------------------------------------------------------------- 1 | //Doubly Linked List 2 | //by Sayed Mahmudul Alam 3 | 4 | //TO DO: import Link.swift 5 | 6 | public class DoublyLinkedList { 7 | var first: Link? 8 | var last: Link? 9 | 10 | init() { 11 | first = nil 12 | last = nil 13 | } 14 | 15 | func isEmpty() -> Bool { 16 | return (first == nil) 17 | } 18 | 19 | func insertFirst(data: Int) { 20 | let link = Link(data:data) 21 | 22 | if(isEmpty()) { 23 | last = link 24 | } else { 25 | first!.previous = link 26 | } 27 | 28 | link.next = first 29 | first = link 30 | } 31 | 32 | func insertLast(data: Int) { 33 | let link = Link(data:data) 34 | 35 | if(isEmpty()) { 36 | first = link 37 | } else { 38 | last!.next = link 39 | } 40 | 41 | link.previous = last 42 | last = link 43 | } 44 | 45 | func insertAfter(key: Int, data: Int) -> Bool { 46 | guard var current = first else { 47 | print("list is empty!!") 48 | return false 49 | } 50 | 51 | while(current.getData() != key) { 52 | if let next = current.next { 53 | current = next 54 | } else { 55 | print("key not found") 56 | return false 57 | } 58 | } 59 | 60 | let link = Link(data: data) 61 | 62 | if(current === last) { 63 | link.next = nil 64 | last = link 65 | } else { 66 | link.next = current.next 67 | current.next!.previous = link 68 | } 69 | 70 | link.previous = current 71 | current.next = link 72 | return true 73 | } 74 | 75 | func deleteFirst() -> Int? { 76 | guard let current = first else { 77 | return nil 78 | } 79 | 80 | if(first!.next == nil) { 81 | last = nil 82 | } else { 83 | first!.next!.previous = nil 84 | } 85 | 86 | first = first!.next 87 | return current.getData() 88 | } 89 | 90 | func deleteLast() -> Int? { 91 | guard let current = last else { 92 | return nil 93 | } 94 | 95 | if(first!.next == nil) { 96 | first = nil 97 | } else { 98 | last!.previous!.next = nil 99 | } 100 | 101 | last = last!.previous 102 | return current.getData() 103 | } 104 | 105 | func delete(key: Int) -> Int? { 106 | guard var current = first else { 107 | print("list is empty!!") 108 | return nil 109 | } 110 | 111 | while(current.getData() != key) { 112 | if let next = current.next { 113 | current = next 114 | } else { 115 | print("key not found") 116 | return nil 117 | } 118 | } 119 | 120 | if(current === first) { 121 | first = current.next 122 | } else { 123 | current.previous!.next = current.next 124 | } 125 | 126 | if(current === last) { 127 | last = current.previous 128 | } else { 129 | current.next!.previous = current.previous 130 | } 131 | 132 | return current.getData() 133 | } 134 | 135 | func displayForward() { 136 | var current = first 137 | 138 | if(current == nil) { 139 | print("list is empty!!") 140 | } else { 141 | while current != nil { 142 | print(current!.getData()) 143 | current = current!.next 144 | } 145 | } 146 | } 147 | 148 | func displayBackward() { 149 | var current = last 150 | 151 | if(current == nil) { 152 | print("List is empty") 153 | } else { 154 | while(current != nil) { 155 | print(current!.getData()) 156 | current = current!.previous 157 | } 158 | } 159 | } 160 | } -------------------------------------------------------------------------------- /DataStructure/LinkedList/DoublyLinkedList/Link.swift: -------------------------------------------------------------------------------- 1 | //Doubly Linked List 2 | //by Sayed Mahmudul Alam 3 | 4 | public class Link { 5 | var data: Int? 6 | var next: Link? 7 | var previous: Link? 8 | 9 | init(data: Int) { 10 | self.data = data 11 | self.next = nil 12 | self.previous = nil 13 | } 14 | 15 | func getData() -> Int { 16 | return data! 17 | } 18 | } -------------------------------------------------------------------------------- /DataStructure/LinkedList/SinglyLinkedList/DoubleEndedLinkedList.swift: -------------------------------------------------------------------------------- 1 | //Double Ended Linked List 2 | //by Sayed Mahmudul Alam 3 | 4 | //TO DO: import Link.swift 5 | 6 | public class DoubleEndedLinkedList { 7 | private var first: Link? 8 | private var last: Link? 9 | 10 | init() { 11 | first = nil 12 | last = nil 13 | } 14 | 15 | func isEmpty() -> Bool { 16 | return first == nil 17 | } 18 | 19 | func inserLast(data: Int) { 20 | let newLink = Link(data: data) 21 | if(isEmpty()) { 22 | first = newLink 23 | } else { 24 | last!.next = newLink 25 | } 26 | last = newLink 27 | } 28 | 29 | func deleteFirst() { 30 | if(first!.next == nil) { 31 | last = nil 32 | } 33 | first = first!.next 34 | } 35 | 36 | func displayList() { 37 | var current = first 38 | 39 | if(current == nil) { 40 | print("List is empty") 41 | } else { 42 | while current != nil { 43 | print(current!.getData()) 44 | current = current!.next 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /DataStructure/LinkedList/SinglyLinkedList/Link.swift: -------------------------------------------------------------------------------- 1 | //Singly Linked List 2 | //by Sayed Mahmudul Alam 3 | 4 | public class Link { 5 | var data: Int? 6 | var next: Link? 7 | 8 | init(data: Int) { 9 | self.data = data 10 | next = nil 11 | } 12 | 13 | func getData() -> Int { 14 | return data! 15 | } 16 | } -------------------------------------------------------------------------------- /DataStructure/LinkedList/SinglyLinkedList/LinkQueue.swift: -------------------------------------------------------------------------------- 1 | //Link Queue 2 | //by Sayed Mahmudul Alam 3 | 4 | //TO DO: import DoubleEndedLinkedList.swift 5 | 6 | public class LinkQueue { 7 | private var list: DoubleEndedLinkedList? 8 | 9 | init() { 10 | list = DoubleEndedLinkedList() 11 | } 12 | 13 | func enqueue(data: Int) { 14 | list!.inserLast(data: data) 15 | } 16 | 17 | func dequeue() { 18 | list!.deleteFirst() 19 | } 20 | 21 | func isEmpty() -> Bool{ 22 | return (list!.isEmpty()) 23 | } 24 | 25 | func displayQueue() { 26 | list!.displayList() 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DataStructure/LinkedList/SinglyLinkedList/LinkStack.swift: -------------------------------------------------------------------------------- 1 | //Link Stack 2 | //by Sayed Mahmudul Alam 3 | 4 | //TO DO: import LinkedList.swift 5 | 6 | public class LinkStack { 7 | private var list: LinkedList? 8 | 9 | init() { 10 | list = LinkedList() 11 | } 12 | 13 | func push(data: Int) { 14 | list!.insertFirst(data: data) 15 | } 16 | 17 | func pop() { 18 | list!.deleteFirst() 19 | } 20 | 21 | func peek() -> Int? { 22 | if list!.isEmpty() { 23 | return nil 24 | } else { 25 | return list!.first!.getData() 26 | } 27 | } 28 | 29 | func displayStack() { 30 | list!.displayList() 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DataStructure/LinkedList/SinglyLinkedList/LinkedList.swift: -------------------------------------------------------------------------------- 1 | //Singly Linked List 2 | //by Sayed Mahmudul Alam 3 | 4 | //TO DO: import Link.swift 5 | 6 | public class LinkedList { 7 | var first: Link? 8 | 9 | init() { 10 | first = nil 11 | } 12 | 13 | func insertFirst(data: Int) { 14 | let newLink = Link(data: data) 15 | newLink.next = first 16 | first = newLink 17 | } 18 | 19 | func deleteFirst() { 20 | if let temp = first { 21 | first = first!.next 22 | print("\(temp.data!) is deleted") 23 | } else { 24 | print("Cannot delete!! List is empty!!") 25 | } 26 | } 27 | 28 | func find(key: Int) -> Link? { 29 | guard var current = first else { 30 | return nil 31 | } 32 | 33 | while current.getData() != key { 34 | if current.next == nil { 35 | return nil 36 | } else { 37 | current = current.next! 38 | } 39 | } 40 | 41 | return current 42 | } 43 | 44 | func delete(key: Int) -> Link?{ 45 | guard var current = first, var previous = first else { 46 | print("List is empty!!") 47 | return nil 48 | } 49 | 50 | while current.getData() != key { 51 | if current.next == nil { 52 | print(current.getData()) 53 | print("called") 54 | return nil 55 | } 56 | else { 57 | previous = current 58 | current = current.next! 59 | } 60 | } 61 | 62 | if(current === first) { 63 | first = first!.next 64 | } else { 65 | previous.next = current.next 66 | } 67 | 68 | return current 69 | } 70 | 71 | func displayList() { 72 | var current = first 73 | 74 | if(current == nil) { 75 | print("List is empty") 76 | } else { 77 | while current != nil { 78 | print(current!.getData()) 79 | current = current!.next 80 | } 81 | } 82 | } 83 | 84 | func isEmpty() -> Bool { 85 | return (first == nil) 86 | } 87 | 88 | func insertSorted(data: Int) { 89 | 90 | let link = Link(data: data) 91 | var previous: Link? = nil 92 | var current = first 93 | 94 | while(current != nil && link.data! > current!.data!) { 95 | previous = current 96 | current = current!.next 97 | } 98 | 99 | if(previous == nil) { 100 | first = link 101 | } else { 102 | previous!.next = link 103 | } 104 | 105 | link.next = current 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /DataStructure/QueueArray.swift: -------------------------------------------------------------------------------- 1 | //Stack 2 | //by Sayed Mahmudul Alam 3 | 4 | struct QueueArray { 5 | 6 | private var queueSize: Int? 7 | private var front = 0 8 | private var rear = -1 9 | private var array: Array 10 | 11 | init(queueSize: Int) { 12 | self.queueSize = queueSize 13 | array = Array(repeating: nil, count: queueSize) 14 | } 15 | 16 | mutating func enqueue(data: Int) { 17 | if rear < queueSize! { 18 | rear += 1 19 | array[rear] = data 20 | } else { 21 | print("Queue is full") 22 | } 23 | } 24 | 25 | mutating func dequeue() -> Int? { 26 | let temp = array[front] 27 | if rear >= 0 { 28 | front += 1 29 | return temp 30 | } else { 31 | return nil 32 | } 33 | } 34 | 35 | mutating func peekFront() -> Int { 36 | return array[front]! 37 | } 38 | 39 | mutating func isEmpty() -> Bool { 40 | return queueSize == 0 41 | } 42 | } -------------------------------------------------------------------------------- /DataStructure/QueueUsingTwoStacks.swift: -------------------------------------------------------------------------------- 1 | //Queue Using two stacks 2 | //by Sayed Mahmudul Alam 3 | 4 | struct QueueUsingTwoStacks { 5 | private var stackSize: Int? 6 | 7 | init(stackSize: Int) { 8 | self.stackSize = stackSize 9 | } 10 | 11 | var stackOne = StackArray(stackSize: 5) 12 | var stackTwo = StackArray(stackSize: 5) 13 | 14 | mutating func enqueue(element: Int) { 15 | stackOne.push(element: element) 16 | } 17 | 18 | mutating func dequeue() -> Int { 19 | while(!stackOne.isEmpty()) { 20 | stackTwo.push(element: stackOne.pop()) 21 | } 22 | return stackTwo.pop() 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /DataStructure/StackArray.swift: -------------------------------------------------------------------------------- 1 | //Stack Array 2 | //Sayed Mahmudul Alam 3 | 4 | struct StackArray { 5 | 6 | private var top = -1 7 | private var stackSize: Int? 8 | private var array: Array 9 | 10 | init(stackSize: Int) { 11 | self.stackSize = stackSize 12 | array = Array(repeating: nil, count: stackSize) 13 | 14 | for i in 0.. Int { 29 | if(top >= 0) { 30 | let temp = array[top] 31 | array[top] = -1 32 | top -= 1 33 | return temp! 34 | } else { 35 | print("Stack underflow") 36 | return -1 37 | } 38 | } 39 | 40 | func getTop() -> Int { 41 | return array[top]! 42 | } 43 | 44 | func isEmpty() -> Bool { 45 | return top == -1 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DataStructure/Tree/BinaryTree/BinaryTree.swift: -------------------------------------------------------------------------------- 1 | //BinaryTree 2 | //Sayed Mahmudul Alam 3 | 4 | class Node { 5 | var value: Int? 6 | var leftChild: Node? 7 | var rightChild: Node? 8 | 9 | init(value: Int) { 10 | self.value = value 11 | leftChild = nil 12 | rightChild = nil 13 | } 14 | } 15 | 16 | class BinaryTree { 17 | private var root: Node? 18 | 19 | init(rootValue: Int) { 20 | root = Node(value: rootValue) 21 | } 22 | 23 | func insertLeft(value: Int) { 24 | var temp = Node(value: value) 25 | if root!.leftChild == nil { 26 | root!.leftChild = temp 27 | } else { 28 | temp.leftChild = root!.leftChild 29 | root!.leftChild = temp 30 | } 31 | } 32 | 33 | func insertRight(value: Int) { 34 | var temp = Node(value: value) 35 | if root!.rightChild == nil { 36 | root!.rightChild = temp 37 | } else { 38 | temp.rightChild = root!.rightChild 39 | root!.rightChild = temp 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [classic-problem-solving-and-data-structures-in-swift] [Sayed Mahmudul Alam] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /ProblemSolving/AddTwoLinkList.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Add two Linked List digits 3 | 4 | //Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 5 | //Output: 7 -> 0 -> 8 6 | 7 | class AddTwoLinkList { 8 | func addBruteForce(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { 9 | var temp1 = l1 10 | var temp2 = l2 11 | var a = 0; 12 | var b = 0; 13 | var mul_a = 1 14 | var mul_b = 1 15 | let dummy: ListNode = ListNode(0) 16 | var output: ListNode? = dummy 17 | 18 | while temp1 != nil { 19 | a += temp1!.val * mul_a 20 | temp1 = temp1!.next 21 | mul_a = mul_a * 10 22 | } 23 | 24 | while temp2 != nil { 25 | b += temp2!.val * mul_b 26 | temp2 = temp2!.next 27 | mul_b = mul_b * 10 28 | } 29 | 30 | var sum = a + b 31 | 32 | while sum != 0 { 33 | var x = ListNode(sum % 10) 34 | output!.next = x 35 | output = output!.next! 36 | sum = sum / 10 37 | 38 | } 39 | 40 | return dummy.next 41 | } 42 | 43 | func add(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { 44 | var temp1 = l1 45 | var temp2 = l2 46 | let dummy: ListNode = ListNode(0) 47 | var output: ListNode? = dummy 48 | var carry = 0 49 | var sumTemp = 0 50 | 51 | while temp1 != nil || temp2 != nil { 52 | 53 | var sum = 0 54 | 55 | if let n = temp1 { 56 | sum = temp1!.val 57 | temp1 = temp1!.next 58 | } 59 | 60 | if let n = temp2 { 61 | sum += temp2!.val 62 | temp2 = temp2!.next 63 | } 64 | 65 | sum += carry 66 | carry = sum / 10 67 | sumTemp = sum 68 | 69 | var x = ListNode(sum % 10) 70 | output!.next = x 71 | output = output!.next! 72 | } 73 | 74 | if sumTemp / 10 == 1 { 75 | output!.next = ListNode(1) 76 | } 77 | 78 | return dummy.next 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /ProblemSolving/AddingArrayDigit.swift: -------------------------------------------------------------------------------- 1 | //Adding two array digits 2 | //Sayed Mahmudul Alam 3 | //input a = [1,2,3,4] b = [1,2] 4 | //output c = [1,2,4,6] 5 | 6 | struct AddingArrayDigit { 7 | func add(array1: [Int], array2: [Int]) -> [Int] { 8 | 9 | var temp = [Int]() 10 | var diff = 0 11 | var sum = 0; 12 | 13 | if(array1.count > array2.count) { 14 | diff = array1.count - array2.count 15 | temp = fillUpArray(array: array2, n: diff) 16 | sum = arrayToInt(array: temp) + arrayToInt(array: array1) 17 | } else if(array2.count > array1.count) { 18 | diff = array2.count - array1.count 19 | temp = fillUpArray(array: array1, n: diff) 20 | sum = arrayToInt(array: temp) + arrayToInt(array: array2) 21 | } else { 22 | sum = arrayToInt(array: array1) + arrayToInt(array: array2) 23 | } 24 | 25 | return intToArray(number: &sum) 26 | } 27 | 28 | private func arrayToInt(array: [Int]) -> Int { 29 | 30 | var number = 0 31 | var mult = 1 32 | 33 | for i in stride(from: array.count - 1, through: 0, by: -1) { 34 | number += array[i] * mult 35 | mult *= 10 36 | } 37 | 38 | return number 39 | 40 | } 41 | 42 | private func intToArray(number: inout Int) -> [Int] { 43 | var result = [Int]() 44 | var temp = 0 45 | 46 | while number != 0 { 47 | temp = number % 10 48 | result.append(temp) 49 | number = number / 10 50 | } 51 | 52 | return result.reversed() 53 | 54 | } 55 | 56 | private func fillUpArray(array: [Int], n: Int) -> [Int] { 57 | var result = [Int]() 58 | 59 | for _ in 0.. Bool { 7 | 8 | guard s.count == t.count else { return false } 9 | 10 | var map = [Character: Int]() 11 | 12 | for char in s { 13 | map[char, default: 0] += 1 14 | } 15 | 16 | for char in t { 17 | if let count = map[char], count > 0 { 18 | map[char] = count - 1 19 | } else { 20 | return false 21 | } 22 | } 23 | return true 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ProblemSolving/ArrayRotate.swift: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-189: https://leetcode.com/problems/rotate-array/description/ 3 | // Example Input: nums = [1,2,3,4,5,6,7], k = 3 4 | // Output: [5,6,7,1,2,3,4] 5 | 6 | struct ArrayRotate { 7 | func rotate(array: [Int], numberOfRotation: Int) -> [Int] {var tempArray = [Int]() 8 | let r = numberOfRotation % array.count 9 | 10 | for i in r.. Bool { 9 | 10 | // if second array is bigger than the first array then it is not a subset 11 | guard array2.count < array1.count else { return false } 12 | 13 | // dictionary to have the count of every element of `array1` 14 | var map = [Int: Int]() 15 | 16 | // add the count of every elemnt of 'array1' to 'map' 17 | array1.forEach { n in 18 | map[n] = map[n] ?? 0 + 1 19 | } 20 | 21 | // check array2's element count in map. 22 | // if a elemnt's count is 0 then 'array2' is not a subset 23 | for i in array2 { 24 | if map[i] ?? 0 > 0 { 25 | map[i] = map[i] ?? 0 - 1 26 | } else { 27 | return false 28 | } 29 | } 30 | 31 | return true 32 | } 33 | -------------------------------------------------------------------------------- /ProblemSolving/BurstBalloons: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-452: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/ 3 | // Solution Ref: https://www.youtube.com/watch?v=uC0aDIhwGdE&t=431s&ab_channel=NareshGupta 4 | // Example Input: points = [[10,16],[2,8],[1,6],[7,12]] 5 | // Output: 2 6 | 7 | class Solution { 8 | func findMinArrowShots(_ points: [[Int]]) -> Int { 9 | 10 | // if input array is empty then no arrow needed 11 | guard !points.isEmpty else { return 0 } 12 | 13 | // sort the array in descending order 14 | let sorted = points.sorted { $0[1] < $1[1] } 15 | 16 | var arrow = 1 17 | var end = sorted[0][1] 18 | 19 | // check if the start of a point is greater then the `end` 20 | // if it's greater then will need another array and 'end' should be updated 21 | for i in 1.. end { 23 | arrow += 1 24 | end = sorted[i][1] 25 | } 26 | } 27 | 28 | return arrow 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ProblemSolving/BuySellStock: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-121: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 2 | // Example Input: prices = [7,1,5,3,6,4] 3 | // Output: 5 4 | 5 | class Solution { 6 | func maxProfit(_ prices: [Int]) -> Int { 7 | var mx = 0 8 | var min = Int.max 9 | 10 | for price in prices { 11 | if price < min { 12 | min = price 13 | } else { 14 | mx = max(mx, price - min) 15 | } 16 | } 17 | 18 | return mx 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ProblemSolving/BuySellStock2: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-122: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 2 | // Example Input: prices = [7,1,5,3,6,4] 3 | // Output: 7 4 | 5 | class Solution { 6 | func maxProfit(_ prices: [Int]) -> Int { 7 | guard !prices.isEmpty else { return 0 } 8 | 9 | var profit = 0 10 | 11 | for i in 1.. prices[i-1] else { continue } 13 | profit += prices[i] - prices[i-1] 14 | } 15 | 16 | return profit 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ProblemSolving/BuySellStock3: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-123: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 2 | // Example Input: prices = [3,3,5,0,0,3,1,4] 3 | // Output: 6 4 | // Ref: https://www.youtube.com/watch?v=YAWRyWJalM0&ab_channel=Fraz 5 | 6 | class Solution { 7 | func maxProfit(_ prices: [Int]) -> Int { 8 | var buy1 = Int.max 9 | var buy2 = Int.max 10 | var sell1 = 0 11 | var sell2 = 0 12 | 13 | for price in prices { 14 | buy1 = min(buy1, price) 15 | sell1 = max(sell1, price - buy1) 16 | 17 | buy2 = min(buy2, price - sell1) 18 | sell2 = max(sell2, price - buy2) 19 | } 20 | 21 | return sell2 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ProblemSolving/CapacityToShipPackagesWithinDDays: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-1011: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 2 | // Example Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5 3 | // Output: 15 4 | 5 | class Solution { 6 | func shipWithinDays(_ weights: [Int], _ days: Int) -> Int { 7 | 8 | var right = weights.reduce(0, +) 9 | var left = weights.max()! 10 | 11 | while left <= right { 12 | let mid = left + (right - left) / 2 13 | if isValid(weights, mid, days) { 14 | right = mid - 1 15 | } else { 16 | left = mid + 1 17 | } 18 | } 19 | 20 | return left 21 | } 22 | 23 | func isValid(_ weights: [Int], _ capacity: Int, _ days: Int) -> Bool { 24 | var d = 1 25 | var sum = 0 26 | 27 | for w in weights { 28 | if sum + w <= capacity { 29 | sum += w 30 | } else { 31 | d += 1 32 | sum = w 33 | } 34 | 35 | if d > days { 36 | return false 37 | } 38 | } 39 | 40 | return true 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ProblemSolving/CommonCharacter: -------------------------------------------------------------------------------- 1 | // Source: https://www.geeksforgeeks.org/check-if-there-is-any-common-character-in-two-given-strings/ 2 | // Example Input: s1 = "geeksforgeeks", s2 = "geeks" 3 | // Output: true 4 | 5 | import Foundation 6 | 7 | class Solution { 8 | 9 | func checkCommon(t1: String, t2: String) -> Bool { 10 | 11 | var map = [Character: Int]() 12 | 13 | for t in t1 { 14 | map[t] = map[t] ?? 0 + 1 15 | } 16 | 17 | for t in t2 { 18 | if map[t] ?? 0 > 0 { 19 | return true 20 | } 21 | } 22 | return false 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ProblemSolving/CountPair: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-2006: https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/submissions/934045089/ 3 | // Example Input: nums = [1,2,2,1], k = 1 4 | // Output: 4 5 | 6 | class Solution { 7 | func countKDifference(_ nums: [Int], _ k: Int) -> Int { 8 | 9 | var count = 0 10 | var seen = [Int: Int]() 11 | 12 | for i in nums { 13 | count += seen[i - k] ?? 0 14 | count += seen[i + k] ?? 0 15 | seen[i, default:0] += 1 16 | } 17 | 18 | return count 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ProblemSolving/CountSpace.swift: -------------------------------------------------------------------------------- 1 | //Count Space Recursion 2 | //by Sayed Mahmudul Alam 3 | 4 | struct CountSpace { 5 | 6 | func count(word: String) -> Int{ 7 | if(word.count == 0 || word.count == 1) { 8 | return 0 9 | } else { 10 | let startIndex = word.index(word.startIndex, offsetBy: 1) 11 | let substring = word[startIndex...] 12 | return (word[word.startIndex] == " " ? 1 : 0) + count(word: String(substring)) 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /ProblemSolving/Fibonacci.swift: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-509: https://www.geeksforgeeks.org/introduction-to-recursion-data-structure-and-algorithm-tutorials/ 3 | // Solution Ref: https://www.section.io/engineering-education/introduction-to-dynamic-programming/#:~:text=Dynamic%20Programming%20(DP)%20is%20an,optimal%20solution%20to%20its%20subproblems. 4 | // Example Input: n = 3 5 | // Output: 2 6 | // Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. 7 | 8 | class RecursiveSolution { 9 | func fib(_ n: Int) -> Int { 10 | if n == 0 { 11 | return 0 12 | } 13 | 14 | if n == 1 || n == 2 { 15 | return 1 16 | } 17 | 18 | return fib(n-1) + fib(n-2) 19 | } 20 | } 21 | 22 | class DPSolution { 23 | func fib(_ n: Int) -> Int { 24 | 25 | guard n > 1 else { 26 | return n 27 | } 28 | 29 | // The first two values of the series are 0 and 1 30 | var first = 0 31 | var second = 1 32 | 33 | // Since first two values are known, the loops can be started from 2 34 | for i in 2...n { 35 | 36 | // current term is the sum of previous two values 37 | let term = first + second 38 | 39 | // Now first term is second term and second term is the current term 40 | first = second 41 | second = term 42 | } 43 | 44 | return second 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ProblemSolving/FindDuplicate: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-287: https://leetcode.com/problems/find-the-duplicate-number/description/ 2 | // Example Input: nums = [1,3,4,2,2] 3 | // Output: 2 4 | 5 | class Solution { 6 | func search(_ nums: [Int], _ target: Int) -> Int { 7 | var low = 0 8 | var high = nums.count - 1 9 | 10 | while low <= high { 11 | let mid = (high + low) / 2 12 | if nums[mid] == target { 13 | return mid 14 | } 15 | if nums[mid] < target { 16 | low = mid + 1 17 | } else if nums[mid] > target { 18 | high = mid - 1 19 | } 20 | } 21 | 22 | return -1 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ProblemSolving/FindGreatestCommonDivisorofArray: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-1979: https://leetcode.com/problems/find-greatest-common-divisor-of-array/description/ 2 | // Example Input: nums = [2,5,6,9,10] 3 | // Output: 2 4 | 5 | class Solution { 6 | func findGCD(_ nums: [Int]) -> Int { 7 | 8 | let min = nums.min()! 9 | let max = nums.max()! 10 | var divisor = min 11 | 12 | while true { 13 | if max % divisor == 0 && min % divisor == 0 { return divisor } 14 | divisor -= 1 15 | } 16 | 17 | return 1 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ProblemSolving/FindIntegers.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Find intergers 3 | 4 | struct FindIntegers { 5 | 6 | //Find two interger that multiplies to a number 7 | func findTwoIntegers(multipliesTo number: Int, array: [Int]) -> (Int, Int)? { 8 | for i in 0.. (Int, Int)? { 36 | for i in 0.. (Int, Int)? { 49 | var map = [Int: Int]() 50 | 51 | for i in 0.. Int { 7 | var n = array.count 8 | var total = (n+1) * (n+2)/2 9 | 10 | for i in 0.. [String] { 8 | 9 | var output = [String]() 10 | 11 | for num in 1...n { 12 | switch (num % 3, num % 5) { 13 | case (0, 0): output.append("FizzBuzz") 14 | case (0, _): output.append("Fizz") 15 | case (_, 0): output.append("Buzz") 16 | default: output.append(String(num)) 17 | } 18 | } 19 | 20 | return output 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ProblemSolving/GCD.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Greatest Common Divisor 3 | 4 | struct GCD { 5 | func getGCD(a: Int, b: Int) -> Int { 6 | let r = a % b 7 | if r != 0 { 8 | return getGCD(a: b, b: r) 9 | } else { 10 | return b 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /ProblemSolving/ImplementQueueUsingStacks: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-283: https://leetcode.com/problems/move-zeroes/description/ 2 | // Example Input: nums = [0,1,0,3,12] 3 | // Output: [1,3,12,0,0] 4 | 5 | class MyQueue { 6 | 7 | private var input: [Int] 8 | private var output: [Int] 9 | 10 | init() { 11 | self.input = [] 12 | self.output = [] 13 | } 14 | 15 | func push(_ x: Int) { 16 | input.append(x) 17 | } 18 | 19 | func pop() -> Int { 20 | if output.isEmpty { 21 | while !input.isEmpty { 22 | output.append(input.last ?? -1) 23 | input.removeLast() 24 | } 25 | } 26 | return output.removeLast() 27 | } 28 | 29 | func peek() -> Int { 30 | if output.isEmpty { 31 | return input.first ?? -1 32 | } 33 | 34 | return output.last ?? -1 35 | } 36 | 37 | func empty() -> Bool { input.isEmpty && output.isEmpty } 38 | } 39 | 40 | /** 41 | * Your MyQueue object will be instantiated and called as such: 42 | * let obj = MyQueue() 43 | * obj.push(x) 44 | * let ret_2: Int = obj.pop() 45 | * let ret_3: Int = obj.peek() 46 | * let ret_4: Bool = obj.empty() 47 | */` 48 | -------------------------------------------------------------------------------- /ProblemSolving/InsertElementAfterEveryNthCharacter: -------------------------------------------------------------------------------- 1 | // Example Input: "11231245" 2 | // Output: "112$312$45" 3 | 4 | class Solution { 5 | func insertElement(to inputText: String, after: Int) -> String { 6 | String(inputText.enumerated().map { $0 > 0 && $0 % after == 0 ? ["$", $1] : [$1]}.joined()) 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ProblemSolving/IntegerPalindrome.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Integer Palindrome 3 | 4 | class IntegerPalindrome { 5 | func isPalindrome(_ x: Int) -> Bool { 6 | 7 | var temp = x 8 | return reverse(temp) == x 9 | } 10 | 11 | func reverse(_ number: Int) -> Int { 12 | var reverseNumber = 0 13 | var tempNumber = number 14 | 15 | while tempNumber != 0 { 16 | reverseNumber = reverseNumber * 10 + tempNumber % 10 17 | tempNumber = tempNumber / 10 18 | } 19 | 20 | return reverseNumber 21 | } 22 | } -------------------------------------------------------------------------------- /ProblemSolving/Intersection.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Find intersection between two array 3 | 4 | struct Intersection { 5 | 6 | func find(array1: [Int], array2: [Int]) -> [Int] { 7 | var set1 = Set() 8 | var set2 = Set() 9 | var result = Array() 10 | 11 | for i in array1 { 12 | set1.insert(i) 13 | } 14 | 15 | for j in array2 { 16 | if(set1.contains(j)) { 17 | set2.insert(j) 18 | } 19 | } 20 | 21 | for k in set2 { 22 | result.append(k) 23 | } 24 | 25 | return result 26 | } 27 | } -------------------------------------------------------------------------------- /ProblemSolving/KClosestPointsToOrigin: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-973: https://leetcode.com/problems/top-k-frequent-elements/description/ 3 | // Example Input: points = [[1,3],[-2,2]], k = 1 4 | // Output: [[-2,2]] 5 | 6 | class Solution { 7 | func kClosest(_ points: [[Int]], _ k: Int) -> [[Int]] { 8 | 9 | // points count must be equal or grater than k 10 | // k must be greater than 0 11 | guard points.count >= k, k > 0 else { 12 | return [[]] 13 | } 14 | 15 | // if points count is equal to k then return 'points' 16 | if points.count == k { 17 | return points 18 | } 19 | 20 | // sort 'points' by distance of each element to the origin 21 | let distanceArray = points.sorted { 22 | return ($0[0] * $0[0]) + ($0[1] * $0[1]) < ($1[0] * $1[0]) + ($1[1] * $1[1]) 23 | } 24 | 25 | // return a prefix of the sorted array having the size of 'k' 26 | return Array(distanceArray.prefix(k)) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ProblemSolving/KthLargestElementInAnArray: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-215: https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 2 | // Example Input: nums = [3,2,1,5,6,4], k = 2 3 | // Output: 5 4 | 5 | class Solution { 6 | func findKthLargest(_ nums: [Int], _ k: Int) -> Int { 7 | if nums.count == 1 { 8 | return nums[0] 9 | } 10 | 11 | let pivot = nums.randomElement()! 12 | 13 | var left = nums.filter { $0 < pivot } 14 | var right = nums.filter { $0 > pivot } 15 | var equal = nums.filter { $0 == pivot } 16 | 17 | if k <= right.count { 18 | return findKthLargest(right, k) 19 | } 20 | 21 | if k > right.count + equal.count { 22 | return findKthLargest(left, k - right.count - equal.count) 23 | } 24 | 25 | return pivot 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ProblemSolving/LinkedListCycle: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-141: https://leetcode.com/problems/linked-list-cycle/description/ 2 | // Solution Ref: https://www.youtube.com/watch?v=agkyC-rbgKM&ab_channel=FisherCoder 3 | // Example head = [3,2,0,-4], pos = 1 4 | // Output: true 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * public class ListNode { 9 | * public var val: Int 10 | * public var next: ListNode? 11 | * public init(_ val: Int) { 12 | * self.val = val 13 | * self.next = nil 14 | * } 15 | * } 16 | */ 17 | 18 | class Solution { 19 | func hasCycle(_ head: ListNode?) -> Bool { 20 | 21 | guard let head = head else { return false } 22 | 23 | var fast = head.next 24 | var slow = head 25 | 26 | while(fast != nil && slow != nil) { 27 | 28 | if fast === slow { return true } 29 | 30 | fast = fast?.next?.next 31 | slow = slow.next! 32 | } 33 | 34 | return false 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ProblemSolving/LongestCommonPrefix.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Find the longest common prefix 3 | 4 | class LongestCommonPrefix { 5 | func find(_ strs: [String]) -> String { 6 | 7 | var minLength = Int.max; 8 | var shortestString:String? 9 | var set = Set() 10 | 11 | for i in strs { 12 | if(i.characters.count < minLength) { 13 | minLength = i.characters.count 14 | shortestString = i 15 | } 16 | } 17 | 18 | if var shortestString = shortestString { 19 | var endIndex = shortestString.endIndex 20 | for str in strs { 21 | while !shortestString.isEmpty && !str.hasPrefix(shortestString) { 22 | endIndex = shortestString.index(before: endIndex) 23 | shortestString = shortestString.substring(to: endIndex) 24 | } 25 | } 26 | return shortestString 27 | } else { 28 | return "" 29 | } 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /ProblemSolving/LongestSub.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Longest Substring Without Repeating Characters 3 | 4 | class LongestSub { 5 | func lengthOfLongestSubstring(_ s: String) -> Int { 6 | 7 | var tempSet = Set() 8 | var tempArray = [Character]() 9 | var count = 0 10 | var countArray = [Int]() 11 | 12 | 13 | for char in s { 14 | tempArray.append(char) 15 | } 16 | 17 | if(tempArray.count == 1) { 18 | return 1 19 | } 20 | 21 | for i in 0.. Int { 38 | 39 | var largest = 0 40 | 41 | for i in array { 42 | if(i > largest) { 43 | largest = i 44 | } 45 | } 46 | 47 | return largest 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /ProblemSolving/LongestSubstringWithoutRepeatingCharacters: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-3: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 2 | // Solution Ref: https://www.youtube.com/watch?v=3IETreEybaA 3 | // Example Input: s = "abcabcbb" 4 | // Output: 3 5 | 6 | class Solution { 7 | func lengthOfLongestSubstring(_ s: String) -> Int { 8 | 9 | var l = 0 10 | var r = 0 11 | var result = 0 12 | var map = Set() 13 | let chars = Array(s) 14 | 15 | while r < chars.count { 16 | if !map.contains(chars[r]) { 17 | map.insert(chars[r]) 18 | r += 1 19 | result = max(map.count, result) 20 | } else { 21 | map.remove(chars[l]) 22 | l += 1 23 | } 24 | } 25 | 26 | return result 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ProblemSolving/LowestCommonAncestorOfABinarySearchTree: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-235: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/ 2 | // Solution Ref: https://www.youtube.com/watch?v=gs2LMfuOR9k&t=334s&ab_channel=NeetCode 3 | // Example Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 4 | // Output: 6 5 | 6 | class Solution { 7 | func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? { 8 | guard let root = root, let p = p, let q = q else { 9 | return nil 10 | } 11 | 12 | var cur = root 13 | 14 | while cur != nil { 15 | if p.val > cur.val && q.val > cur.val { 16 | cur = cur.right! 17 | } else if p.val < cur.val && q.val < cur.val { 18 | cur = cur.left! 19 | } else { 20 | return cur 21 | } 22 | } 23 | 24 | return nil 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ProblemSolving/LowestCommonAncestorOfABinaryTree: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-236: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/submissions/ 2 | // Solution Ref: https://www.youtube.com/watch?v=WO1tfq2sbsI&ab_channel=CrackingFAANG 3 | // Example Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 4 | // Output: 3 5 | 6 | /** 7 | * Definition for a binary tree node. 8 | * public class TreeNode { 9 | * public var val: Int 10 | * public var left: TreeNode? 11 | * public var right: TreeNode? 12 | * public init(_ val: Int) { 13 | * self.val = val 14 | * self.left = nil 15 | * self.right = nil 16 | * } 17 | * } 18 | */ 19 | 20 | class Solution { 21 | func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? { 22 | 23 | guard let root = root, let p = p, let q = q else { 24 | return nil 25 | } 26 | 27 | if root.val == p.val || root.val == q.val { 28 | return root 29 | } 30 | 31 | let leftSearch = lowestCommonAncestor(root.left, p, q) 32 | let rightSearch = lowestCommonAncestor(root.right, p, q) 33 | 34 | if let foundLeft = leftSearch, let foundRight = rightSearch { 35 | return root 36 | } 37 | 38 | return leftSearch ?? rightSearch 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ProblemSolving/MaxSecondMax: -------------------------------------------------------------------------------- 1 | //Max and Second Max 2 | //by Sayed Mahmudul Alam 3 | 4 | func getMaxAndSecondMax(array: [Int]) -> (Int, Int) { 5 | var max = 0 6 | var sMax = 0 7 | for i in 0...array.count - 1 { 8 | if(array[i] > max) { 9 | sMax = max 10 | max = array[i] 11 | } else if(array[i] > sMax) { 12 | sMax = array[i] 13 | } 14 | } 15 | 16 | return (max,sMax) 17 | } -------------------------------------------------------------------------------- /ProblemSolving/MergeTwoSortedList: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-21: https://leetcode.com/problems/merge-two-sorted-lists/description/ 2 | // Example Input: list1 = [1,2,4], list2 = [1,3,4] 3 | // Output: [1,1,2,3,4,4] 4 | 5 | class Solution { 6 | func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { 7 | if list1 == nil || list2 == nil { 8 | return list1 == nil ? list2 : list1 9 | } 10 | 11 | if list1!.val <= list2!.val { 12 | list1?.next = mergeTwoLists(list1?.next, list2) 13 | return list1 14 | } else { 15 | list2?.next = mergeTwoLists(list1, list2?.next) 16 | return list2 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ProblemSolving/MinimumDifference: -------------------------------------------------------------------------------- 1 | func minimumDifference(_ array: [Int]) -> Int { 2 | let sorted = array.sorted { 3 | $0 < $1 4 | } 5 | 6 | var diff = 1000 7 | 8 | for i in 0.. [Int] { 7 | var answer = [Int]() 8 | var chars = Array(s) 9 | var matchedIndices = [Int]() 10 | 11 | for(index, char) in chars.enumerated() { 12 | if char == c { matchedIndices.append(index) } 13 | } 14 | 15 | var low = 0 16 | var high = 0 17 | var index = 0 18 | 19 | if matchedIndices.count > 1 { high = 1 } 20 | 21 | while index < chars.count { 22 | let lowIndexValue = abs(index - matchedIndices[low]) 23 | let highIndexValue = abs(index - matchedIndices[high]) 24 | let minValue = min(lowIndexValue, highIndexValue) 25 | answer.append(minValue) 26 | 27 | if highIndexValue == minValue { 28 | if low < matchedIndices.count - 1 { low += 1 } 29 | if high < matchedIndices.count - 1 { high += 1 } 30 | } 31 | 32 | index += 1 33 | 34 | } 35 | 36 | return answer 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ProblemSolving/Palindrome.swift: -------------------------------------------------------------------------------- 1 | //Palindrome Recursion 2 | //by Sayed Mahmudul Alam 3 | 4 | struct Palindrome { 5 | 6 | func isPalindrome(word: String) -> Bool { 7 | if(word.count == 0 || word.count == 1) { 8 | return true; 9 | } else if(word[word.startIndex] == word[word.index(before: word.endIndex)]) { 10 | let startIndex = word.index(word.startIndex, offsetBy: 1) 11 | let endIndex = word.index(word.endIndex, offsetBy: -1) 12 | let substring = word[startIndex.. Bool { 7 | 8 | /// If s2 is smaller than s1, return false 9 | guard s2.count >= s1.count else { 10 | return false 11 | } 12 | 13 | var l = 0 14 | var r = s1.count - 1 15 | 16 | let s1 = Array(s1) 17 | let s2 = Array(s2) 18 | 19 | /// Create dictionary for frequency of s2 substring and frequency of s1 substring 20 | var subFreq = [Character: Int]() 21 | var s1Freq = [Character: Int]() 22 | 23 | /// Initialize the dictionaries with the frequency of characters in the first substring of s2 24 | for i in 0.. [Int] { 7 | 8 | var temp = digits 9 | 10 | for i in (0.. Bool { 7 | var i = 2 8 | while i < number { 9 | if number % i == 0 { 10 | return false 11 | } 12 | i += 1 13 | } 14 | return true 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ProblemSolving/PrintPatterns.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Patterns 3 | 4 | struct PrintPatterns { 5 | 6 | func printPatternA(n: Int) { 7 | for i in 0.. Int { 8 | guard nums.count > 0 else { return 0 } 9 | 10 | var l = 1, r = 1 11 | 12 | for i in 1.. Int { 6 | var tempSet = Set() 7 | 8 | for (i,num) in nums.enumerated().reversed() { 9 | if(!tempSet.insert(nums[i]).0) { 10 | nums.remove(at: i) 11 | } 12 | } 13 | 14 | return nums.count 15 | } 16 | } -------------------------------------------------------------------------------- /ProblemSolving/ReversWordsInString: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-151: https://leetcode.com/problems/reverse-words-in-a-string/description/ 2 | // Example Input: s = "the sky is blue" 3 | // Output: "blue is sky the" 4 | 5 | class Solution { 6 | func reverseWords(_ s: String) -> String { 7 | var rev = s.split(separator: " ") 8 | var start = 0, end = rev.count - 1 9 | 10 | while start < end { 11 | rev.swapAt(start, end) 12 | start += 1 13 | end -= 1 14 | } 15 | 16 | return rev.joined(separator: " ") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ProblemSolving/ReverseArrayOfString: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-344: https://leetcode.com/problems/reverse-string/description/ 3 | // Example Input: s = ["h","e","l","l","o"] 4 | // Output: ["o","l","l","e","h"] 5 | 6 | class Solution { 7 | func reverseString(_ s: inout [Character]) { 8 | guard !s.isEmpty else { return } 9 | 10 | var l = 0 11 | var r = s.count - 1 12 | 13 | while l < r { 14 | let temp = s[l] 15 | s[l] = s[r] 16 | s[r] = temp 17 | l += 1 18 | r -= 1 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ProblemSolving/ReverseInteger.swift: -------------------------------------------------------------------------------- 1 | //Reverse Integar 2 | //by Sayed Mahmudul Alam 3 | 4 | struct ReverseInteger { 5 | 6 | func reverse(number: Int) -> Int { 7 | var reverseNumber = 0 8 | var tempNumber = number 9 | 10 | while tempNumber != 0 { 11 | reverseNumber = reverseNumber * 10 + tempNumber % 10 12 | tempNumber = tempNumber / 10 13 | } 14 | 15 | return reverseNumber 16 | } 17 | } -------------------------------------------------------------------------------- /ProblemSolving/ReverseLinkedList: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-206: https://leetcode.com/problems/reverse-linked-list/description/ 2 | // Solution Ref: https://www.youtube.com/watch?v=NhapasNIKuQ&ab_channel=NickWhite 3 | // Example Input: head = [1,2,3,4,5] 4 | // Output: [5,4,3,2,1] 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * public class ListNode { 9 | * public var val: Int 10 | * public var next: ListNode? 11 | * public init() { self.val = 0; self.next = nil; } 12 | * public init(_ val: Int) { self.val = val; self.next = nil; } 13 | * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } 14 | * } 15 | */ 16 | 17 | class Solution { 18 | func reverseList(_ head: ListNode?) -> ListNode? { 19 | 20 | var prev: ListNode? = nil 21 | var h = head 22 | 23 | while(h != nil) { 24 | // get reference to the next node 25 | var next = h?.next 26 | // make next refers to prev 27 | h?.next = prev 28 | // update previous node as head 29 | prev = h 30 | // move head to the next node 31 | h = next 32 | } 33 | 34 | return prev 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ProblemSolving/ReverseWord.swift: -------------------------------------------------------------------------------- 1 | //Reverse Word 2 | //by Sayed Mahmudul Alam 3 | 4 | struct ReverseWord { 5 | 6 | func reverseRecursion(word: String) -> String { 7 | if(word.count == 0 || word.count == 1) { 8 | return word 9 | } else { 10 | let startIndex = word.index(word.startIndex, offsetBy: 1) 11 | let substring = word[startIndex...] 12 | return reverseWordRecursion(word: String(describing: substring)) + String(describing: 13 | word[word.startIndex]) 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ProblemSolving/RomanToInteger.swift: -------------------------------------------------------------------------------- 1 | //Sayed Mahmudul Alam 2 | //Roman to Integer 3 | 4 | class RomanToInteger { 5 | func convert(_ s: String) -> Int { 6 | 7 | var dic: [Character: Int] = ["I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000] 8 | var tempArray = [Character]() 9 | var output = 0 10 | 11 | for char in s { 12 | tempArray.append(char) 13 | } 14 | 15 | for i in 0.. dic[tempArray[i + 1]]! || dic[tempArray[i]]! == dic[tempArray[i + 1]]!) { 20 | output += dic[tempArray[i]]! 21 | } else if(dic[tempArray[i]]! < dic[tempArray[i + 1]]!) { 22 | output -= dic[tempArray[i]]! 23 | } 24 | } 25 | 26 | return output 27 | } 28 | } -------------------------------------------------------------------------------- /ProblemSolving/RotateImage: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-48: https://leetcode.com/problems/rotate-image/description/ 2 | // Example Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 3 | // Output: [[7,4,1],[8,5,2],[9,6,3]] 4 | 5 | class Solution { 6 | func rotate(_ matrix: inout [[Int]]) { 7 | 8 | /// Transpose 9 | 10 | for i in 0.. Int { 8 | var min = Int.max 9 | var position = 0 10 | 11 | if(target > nums.last!) { 12 | return nums.count 13 | } 14 | 15 | if(target < nums.first!) { 16 | return 0 17 | } 18 | 19 | for i in 0.. nums[i]) { 30 | position = i + 1 31 | } else if(target < nums[i]){ 32 | position = i 33 | } 34 | } 35 | } 36 | return position 37 | } 38 | } -------------------------------------------------------------------------------- /ProblemSolving/SumOfAllDigits.swift: -------------------------------------------------------------------------------- 1 | //Sum of All Digits 2 | //Sayed Mahmudul Alam 3 | 4 | struct SumOfAllDigits { 5 | private var sum = 0 6 | 7 | mutating func calculate(number: Int) -> Int { 8 | if number == 0 { 9 | return sum 10 | } else { 11 | sum += (number % 10) 12 | calculate(number: number / 10) 13 | } 14 | 15 | return sum 16 | } 17 | 18 | func getSum(number: inout Int) -> Int { 19 | var sum = 0 20 | 21 | while number != 0 { 22 | sum += number % 10 23 | number = number / 10 24 | } 25 | 26 | return sum 27 | } 28 | } -------------------------------------------------------------------------------- /ProblemSolving/SwapWithoutTemp.swift: -------------------------------------------------------------------------------- 1 | //Swap Without Temp 2 | //Sayed Mahmudul Alam 3 | 4 | struct SwapWithoutTemp { 5 | func swap(a : inout Int, b: inout Int) -> (Int, Int) { 6 | 7 | a = a + b 8 | b = a - b 9 | a = a - b 10 | 11 | return (a,b) 12 | } 13 | } -------------------------------------------------------------------------------- /ProblemSolving/TopKFrequentElements: -------------------------------------------------------------------------------- 1 | // Author: Sayed Mahmudul Alam 2 | // Source: LeetCode-347: https://leetcode.com/problems/top-k-frequent-elements/description/ 3 | // Solution Ref: https://www.youtube.com/watch?v=YPTqKIgVk-k&ab_channel=NeetCode 4 | // Example Input: nums = [1,1,1,2,2,3], k = 2 5 | // Output: [1,2] 6 | 7 | class Solution { 8 | func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { 9 | 10 | // Dictionary to hold count of each element 11 | var count = [Int: Int]() 12 | 13 | // Frequency table to hold the frequency of each number 14 | var freq = [[Int]](repeating: [], count: nums.count + 1) 15 | 16 | // Array to hold the result 17 | var result = [Int]() 18 | 19 | // Populating the `count` dictionary with actual counts 20 | nums.forEach { n in 21 | count[n] = 1 + (count[n] ?? 0) 22 | } 23 | 24 | // Populate the `freq` table with elements for each number 25 | for (_,value) in count.enumerated() { 26 | freq[value.1].append(value.0) 27 | } 28 | 29 | // Loop through `freq` in reverse and add the element of inner array to `result` 30 | // till `result` is equal `k` 31 | let sequence = stride(from: freq.count - 1, to: 0, by: -1) 32 | for i in sequence { 33 | for j in freq[i] { 34 | result.append(j) 35 | if result.count == k { 36 | return result 37 | } 38 | } 39 | } 40 | 41 | return [] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ProblemSolving/TwoSum: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-1: https://leetcode.com/problems/two-sum/description/ 2 | // Example Input: numbers = [2,7,11,15], target = 9 3 | // Output: [1, 2] 4 | 5 | class Solution { 6 | func twoSum(_ nums: [Int], _ target: Int) -> [Int] { 7 | var map = [Int: Int]() 8 | 9 | for (k,v) in nums.enumerated() { 10 | if let key = map[target - v] { 11 | return [k, key] 12 | } else { 13 | map[v] = k 14 | } 15 | } 16 | 17 | return [] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ProblemSolving/TwoSumSortedArray: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-167: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | // Example Input: numbers = [2,7,11,15], target = 9 3 | // Output: [1, 2] 4 | 5 | class Solution { 6 | func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { 7 | 8 | var l = 0, r = numbers.count - 1 9 | 10 | while l <= r { 11 | let sum = numbers[l] + numbers[r] 12 | if sum == target { 13 | return [l + 1, r + 1] 14 | } else if sum < target { 15 | l += 1 16 | } else { 17 | r -= 1 18 | } 19 | } 20 | 21 | return [] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ProblemSolving/ValidPalindrome: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-125: https://leetcode.com/problems/valid-palindrome/description/ 2 | // Example Input: s = "A man, a plan, a canal: Panama" 3 | // Output: true 4 | 5 | class Solution { 6 | func isPalindrome(_ s: String) -> Bool { 7 | 8 | guard !s.isEmpty else { return true } 9 | 10 | let s = Array(s.lowercased().filter { $0.isLetter || $0.isNumber }) 11 | 12 | var left = 0 13 | var right = s.count - 1 14 | 15 | while left <= right { 16 | if s[left] != s[right] { return false } 17 | left += 1 18 | right -= 1 19 | } 20 | 21 | return true 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ProblemSolving/ValidParentheses.swift: -------------------------------------------------------------------------------- 1 | // Source: LeetCode-20: https://leetcode.com/problems/valid-parentheses/description/ 2 | // Example Input: s = "()[]{}" 3 | // Output: true 4 | 5 | class ValidParentheses { 6 | func isValid(_ s: String) -> Bool { 7 | 8 | var temp = [Character]() 9 | 10 | for i in s { 11 | 12 | if(i == "(" || i == "{" || i == "[") { 13 | 14 | temp.append(i) 15 | 16 | } else if(i == "]") { 17 | 18 | if(temp.isEmpty || temp.last != "[") { 19 | return false 20 | } else { 21 | temp.removeLast() 22 | } 23 | } else if(i == "}") { 24 | 25 | if(temp.isEmpty || temp.last != "{") { 26 | return false 27 | } else { 28 | temp.removeLast() 29 | } 30 | } else if(i == ")") { 31 | 32 | if(temp.isEmpty || temp.last != "(") { 33 | return false 34 | } else { 35 | temp.removeLast() 36 | } 37 | } 38 | } 39 | 40 | return temp.isEmpty 41 | 42 | } 43 | 44 | func isValidShort(_ s: String) -> Bool { 45 | 46 | guard s.count % 2 == 0 else { return false } 47 | 48 | var stack: [Character] = [] 49 | 50 | for item in s { 51 | switch item { 52 | case "(": stack.append(")") 53 | case "{": stack.append("}") 54 | case "[": stack.append("]") 55 | default: 56 | if stack.isEmpty || stack.removeLast() != item { 57 | return false 58 | } 59 | } 60 | } 61 | 62 | return stack.isEmpty 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # classic-problem-solving-and-data-structures-in-swift 2 | 3 | Resource for solving data structures and algorithms using swift is growing rapidly now-a-days. This repo is a small effort 4 | toward that process. Feel free to contribute. Cheers. 5 | 6 | **Algorithm** 7 | * Search 8 | * [Binary Search](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/Algorithms/Search/BinarySearch) 9 | * [Linear Search](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/tree/master/Algorithms/Search/LinearSearch.playground) 10 | * Sort 11 | * [Bubble Sort](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/Algorithms/Sort/BubbleSort) 12 | * [Selection Sort](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/tree/master/Algorithms/Sort/SelectionSort.playground) 13 | * [Selection Sort - Alternative](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/tree/master/Algorithms/Sort/SelectionSortAlt.playground) 14 | * [Insertion Sort](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/tree/master/Algorithms/Sort/InsertionSort.playground) 15 | * [Merge Sort](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/tree/master/Algorithms/Sort/MergeSort.playground) 16 | 17 | **Data Structure** 18 | * Linked List 19 | * [Singly Linked List](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/tree/master/DataStructure/LinkedList/SinglyLinkedList) 20 | * [DoubleEndedLinkedList](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/LinkedList/SinglyLinkedList/DoubleEndedLinkedList.swift) 21 | * [Link Queue](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/LinkedList/SinglyLinkedList/LinkQueue.swift) 22 | * [Link Stack](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/LinkedList/SinglyLinkedList/LinkStack.swift) 23 | * [Linked List](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/LinkedList/SinglyLinkedList/LinkedList.swift) 24 | * [Doubly Linked List](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/tree/master/DataStructure/LinkedList/DoublyLinkedList) 25 | * [Linked List](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/LinkedList/DoublyLinkedList/DoublyLinkedList.swift) 26 | * Stack/Queue 27 | * [Queue Array](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/QueueArray.swift) 28 | * [Stack Array](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/StackArray.swift) 29 | * [Queue using two stacks](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/QueueUsingTwoStacks.swift) 30 | * Tree 31 | * [Binary Tree](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/DataStructure/Tree/BinaryTree/BinaryTree.swift) 32 | 33 | **Problem Solving** 34 | * [Adding Array Digits](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/AddingArrayDigit.swift) 35 | * [Adding LinkedList Digits](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/AddTwoLinkList.swift) 36 | * [Array Intersection](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/Intersection.swift) 37 | * [Array Subset](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/ArraySubset) 38 | * [Anargram](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/Anargram.swift) 39 | * [Array Rotation](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/ArrayRotate.swift) 40 | * [Burst Ballons](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/BurstBalloons) 41 | * [Buy sell stock](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/BuySellStock) 42 | * [Buy sell stock 2](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/BuySellStock2) 43 | * [Buy sell stock 3](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/BuySellStock3) 44 | * [Capacity To Ship Packages Within D Days](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/CapacityToShipPackagesWithinDDays) 45 | * [Common Character](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/CommonCharacter) 46 | * [Count Pair](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/CountPair) 47 | * [Count Space](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/CountSpace.swift) 48 | * [Nearest Distance To Character](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/NearestDistanceToCharacter) 49 | * [Fibonacci](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/Fibonacci.swift) 50 | * [Find Interger](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/FindIntegers.swift) 51 | * [Find The Missing Number](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/FindTheMissingNumber.swift) 52 | * [Find Duplicate](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/FindDuplicate) 53 | * [Find Greatest Common Divisor of Array](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/FindGreatestCommonDivisorofArray) 54 | * [Fizz Buzz](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/FizzBuzz) 55 | * [Greatest Common Divisor ](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/GCD.swift) 56 | * [Implement Queue using Stacks](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/ImplementQueueUsingStacks) 57 | * [Insert Element After Every Nth Character](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/InsertElementAfterEveryNthCharacter) 58 | * [Interger Palindrome](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/IntegerPalindrome.swift) 59 | * [Intersection](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/Intersection.swift) 60 | * [K Closest Points to Origin](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/KClosestPointsToOrigin) 61 | * [Kth Largest Element in an Array](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/KthLargestElementInAnArray) 62 | * [Linked List Cycle](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/LinkedListCycle) 63 | * [Longest Substring](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/LongestSub.swift) 64 | * [Longest Substring Without Repeating Characters](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/LongestSubstringWithoutRepeatingCharacters) 65 | * [Longest Common Sunstring](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/LongestCommonPrefix.swift) 66 | * [Lowest Common Ancestor of a Binary Search Tree](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/LowestCommonAncestorOfABinarySearchTree) 67 | * [Lowest Common Ancestor of a Binary Tree](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/LowestCommonAncestorOfABinaryTree) 68 | * [Max and Second Max](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/MaxSecondMax) 69 | * [Merge two sorted list](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/MergeTwoSortedList) 70 | * [Minimum Difference](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/MinimumDifference) 71 | * [Move Zeroes](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/MoveZeroes) 72 | * [Palindrome](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/Palindrome.swift) 73 | * [Permutation in String](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/PermutationInString) 74 | * [Plus One](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/PlusOne) 75 | * [Prime Number](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/PrimeNumber.swift) 76 | * [Print Patterns](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/PrintPatterns.swift) 77 | * [Remove Duplicates From Sorted Array](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/RemoveDuplicateSortedArray) 78 | * [Remove Duplicate](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/RemoveDuplicates.swift) 79 | * [Reverse Words in String](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/ReversWordsInString) 80 | * [Reverse Array of String](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/ReverseArrayOfString) 81 | * [Reverse Integer](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/ReverseInteger.swift) 82 | * [Reverse Linked List](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/ReverseLinkedList) 83 | * [Reverse Word](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/ReverseWord.swift) 84 | * [Roman to Integer](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/RomanToInteger.swift) 85 | * [Rotate Image](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/RotateImage) 86 | * [Search Insert Position](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/SearchInsertPosition.swift) 87 | * [Sum of All Digit](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/SumOfAllDigits.swift) 88 | * [Swap Without Temp](https://github.com/smalam119/classic-problem-solving-and-data-structures-in-swift/blob/master/ProblemSolving/SwapWithoutTemp.swift) 89 | * [Top K Frequent Elements](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/TopKFrequentElements) 90 | * [Two Sum](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/TwoSum) 91 | * [Two Sum Sorted Array](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/TwoSumSortedArray) 92 | * [Valid Palindrome](https://github.com/smalam119/popular-algorithms-data-structures-and-problems-in-swift/blob/master/ProblemSolving/ValidPalindrome) 93 | * [Valid Parentheses](https://github.com/smalam119/classic-problem-solving-algorithms-and-data-structures-in-swift/blob/master/ProblemSolving/ValidParentheses.swift) 94 | 95 | Author 96 | ======= 97 | * **Sayed Mahmudul Alam** 98 | 99 | Reference 100 | ======= 101 | * Robert Lafore, *Data Structures & Algorithms (2nd ed.)* 102 | * LeetCode 103 | * Online resource 104 | 105 | License 106 | ======= 107 | 108 | Copyright 2018 Sayed Mahmudul Alam 109 | 110 | Licensed under the Apache License, Version 2.0 (the "License"); 111 | you may not use this file except in compliance with the License. 112 | You may obtain a copy of the License at 113 | 114 | http://www.apache.org/licenses/LICENSE-2.0 115 | 116 | Unless required by applicable law or agreed to in writing, software 117 | distributed under the License is distributed on an "AS IS" BASIS, 118 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 119 | See the License for the specific language governing permissions and 120 | limitations under the License. 121 | --------------------------------------------------------------------------------