Skip to main content

Subsequence check

A subsequence of a string is a new string that is formed from the original string by removing zero or more characters without disturbing the relative positions of the remaining characters.

Ex: Original String = ABC
Subsequences = "", A, B, C, AB, AC, BC, ABC

Characteristics of a Subsequence:
1. Length of subsequence is always less than or equal to the original string.
2. Characters should always appear in the same order as in the original string.
3. The total number of subsequences of a string with length n will be 2nSince for every character we have 2 choices, either to keep it or remove it.

func checkIs(subsequence:String, from input:String)->Bool{
    guard subsequence.count <= input.count else { return false }
    var subsequenceIndex = subsequence.startIndex
    var inputIndex = subsequence.startIndex
    while inputIndex < input.endIndex &&
            subsequenceIndex < subsequence.endIndex {
        if subsequence[subsequenceIndex] == input[inputIndex]{
            subsequenceIndex = subsequence.index(after: subsequenceIndex)
        inputIndex = input.index(after: inputIndex)
    return subsequenceIndex == subsequence.endIndex
Time Complexity: O(n+m)
Space Complexity: O(1)


Popular posts from this blog

UILabel text animation as in UIButton click

Animating UILabel textcolor is not easy as it seems, normal animation won't work on this property. The below code snippet animates the UILabel textcolor as in button click. Change the duration and UIViewAnimationOptions to play with it. extension UILabel { func flashLabel() { func animate(duration: Double ,alpha: CGFloat ,completion:(()->())?){ UIView . transition (with: self , duration: duration, options: UIViewAnimationOptions . transitionCrossDissolve , animations: { self . textColor = self . textColor ?. withAlphaComponent (alpha) }) { _ in completion?() } } animate (duration: 0.1 , alpha: 0 ) { animate (duration: 0.3 , alpha: 1 , completion: nil ) } } } Happy coding...

Confused OS Concepts

Difference between MultiProcessor and Multicore Processor? A  multiprocessor   system  contains more than one CPU, allowing them to work in parallel. A  multicore   CPU  has multiple execution cores on one CPU so that multiple cores can work in parallel on separate operations at chip level. Difference between MultiProgramming, MultiTasking, MultiProcessing,      MultiThreading? MultiProgramming: To minimize the CPU Idle time, Multiprogramming was introduced in older days for single core processor. One or more programs are loaded into main memory which are ready to execute still, only one program is able to get the CPU for execution while all others are waiting for their turn. When currently running process is performing an I/O task, then OS may interrupt that process and give the control to another program( process context switching ) loaded in main memory for execution and a running process keeps executing until either it voluntarily releases the CPU or when it blocks for an I

Design Patterns in Swift

What is a Design pattern? A design pattern is a general solution to a real-world problem that can be re-used. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. They are templates designed to help us write efficient code which is easy to understand, also can be re-used. It is not a finished design that can be transformed directly into source or machine code. Why do we need Design patterns? Saves Time : Design patterns can speed up the development process by providing tested, proven development paradigms Future bugs : Effective design pattern can solve issues that may not become visible until later in the implementation, which helps while designing software. Re-use : Reusing design patterns helps to prevent subtle issues that can cause major problems. Code Readability : Improves code readability for coders and architects familiar with the patterns. Best practices : Design patterns teaches