Category: Swift

Implement Merge Intervals in Swift

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4]

Continue Reading →

Accessing non static constant in static function of swift class

I came across this question on stackoverflow. https://stackoverflow.com/questions/32346456/accessing-non-static-constant-in-static-function-of-swift-class Basically it asked, how we can access instance members from static functions. The answer would be no, we can’t access instance variables from static method. But this is possible using a singleton design pattern. The actual code is shown below This is how I can alter it.

Continue Reading →

Find the largest number by swapping adjacent digits if they have the same parity in Swift

Problem description: You are given a number. You can swap two adjacent digits, if they are both odd or even. For e.g. given (3,8), you can’t swap them. But you can swap (5,9) since they are both odd. They have the same parity. A similar problem is explained here https://www.youtube.com/watch?v=qEIGhVtZ-sg Function description: swapDigitsAndGetLargestNumber(number: String) Returns:

Continue Reading →

Word ladder implementation in Swift

https://leetcode.com/problems/word-ladder/ This post contains the implementation for the leetcode problem shown in the link above. A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> … -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.

Continue Reading →

Permutations of a string in Swift

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique.

Continue Reading →

Magical Candy Bags

Magical Candy BagsYou have N bags of candy. The ith bag contains arr[i] pieces of candy, and each of the bags is magical!It takes you 1 minute to eat all of the pieces of candy in a bag (irrespective of how many pieces of candy are inside), and as soon as you finish, the bag

Continue Reading →

Implementing UIScrollView using constraints without using content size

UIScrollView is a view that allows the scrolling and zooming of its contained views. UIScrollView is the superclass of several UIKit classes including UITableView and UITextView. The central notion of a UIScrollView object (or, simply, a scroll view) is that it is a view whose origin is adjustable over the content view. It clips the content to its frame, which generally

Continue Reading →

Struct vs Class in Swift

If you look at structs and classes in Swift, they seem similar, but the most fundamental difference between them is how they handle values passed to them. In general Struct is pass by value, whereas Class is pass by reference. I will explain the difference by showing an example. In the above code, I have

Continue Reading →

Method swizzling in Swift and ObjectiveC

Method Swizzling is the ability that Objective-C Runtime gives us, to switch the implementation of an existing selector at runtime. I came across an article in NSHipster which does method swizzling. The link to the article is here https://nshipster.com/swift-objc-runtime/ For convenience or to work around a bug in a framework, or because there’s just no

Continue Reading →

Reading and Writing to a JSON in Swift

Lets consider you have a sample JSON as shown belowhttps://gist.github.com/kmdarshan/cfa6a940268f7091faf886ed63b3b559 You can make use of Codable Library (https://developer.apple.com/documentation/swift/codable) in Swift to read and write data from a JSON. Firstly you need to map your data from the JSON onto a Struct as shown below. Once you have done that, its easy to read it using

Continue Reading →

Largest Triple Products in Swift

Largest Triple Products You’re given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer

Continue Reading →