-
Continue reading →: Memento Design Pattern
Memento: something that you keep to remind you of somebody/something Imagine you’re working on a drawing application. You make some strokes, but then you decide you don’t like them. The Memento pattern allows you to capture the state of your drawing (the color, line thickness, strokes made so far) at…
-
Continue reading →: State Design Pattern
The State Pattern allows an object to alter its behavior when its internal state changes. It achieves this by defining an interface (or abstract base class) for various states and concrete classes for each specific state. The context object holds a reference to the current state and delegates behavior based…
-
Continue reading →: Mediator Design Pattern
Mediator pattern can be used when colleague need to talk to each other, without talking to each other.c1 (colleague 1 ) talks to Mediator -> If needed Mediator communicates to c2. Follow more posts @ https://jdecodes.wordpress.com My all design pattern articles :
-
Continue reading →: Iterator design pattern
Summary of Iterator Pattern Usage:Purpose: Use the Iterator pattern when you need to iterate over elements of a collectionwithout exposing the internal structure to the client code. Implementation Approaches: Custom Iterator (hasNext()/next()): Approach 1 Diagram approach 2 diagram Code 1 : Approach 1: client code from approach 1: Approach 2…
-
Continue reading →: Command Design Pattern
Official defintion The Command Pattern is a behavioral design pattern that focuses on encapsulating a request asan object, thereby decoupling the sender of the request from the receiver. This patternallows you to parameterize objects with commands, delay or queue a request’s execution, andsupport undoable operations. It’s a fundamental pattern for…
-
Continue reading →: Builder Design Pattern
When you want to create a complex object,1 way is to pass too many arguments in the constructor,have multiple constructors.this makes the code error prone. Other Method, is you create object step by step, so builder pattern can be used. // Builder will have the API to set underlying object’s…
-
Continue reading →: Longest Repeating Character Replacement
https://leetcode.com/problems/longest-repeating-character-replacement/description Example 1: Input: s = “ABAB”, k = 2 Output: 4 Explanation: Replace the two ‘A’s with two ‘B’s or vice versa. Example 2: Input: s = “AABABBA”, k = 1Output: 4Explanation: Replace the one ‘A’ in the middle with ‘B’ and form “AABBBBA”.The substring “BBBB” has the longest…
-
Continue reading →: Fruits into basket
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:…
-
Continue reading →: Max Consecutive 1’s
Given a binary array nums and an integer k, return the maximum number of consecutive 1‘s in the array if you can flip at most k 0‘s. Example 1: Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: nums…
-
Continue reading →: Longest Substring Without Repeating Characters
Given a string S, find the length of the longest substring without repeating characters. Input:S = “geeksforgeeks”Output:7Explanation:Longest substring is”eksforg”.https://www.geeksforgeeks.org/problems/length-of-the-longest-substring3036https://leetcode.com/problems/longest-substring-without-repeating-characters