We will implement the same algorithm in approach 1 using recursive method.
The trick is that each time a recursive function calls itself, it reduces the given problem into subproblems. The recursion call continues until it reaches a point where the subproblem can be solved without further recursion.
In this problem, once we find a pair that should be deleted, we are actually reducing s into a new string s' which is 2 characters smaller. Then the function calls itself for this smaller subproblem. When we can't find a pair for s, we have reached the base case where the problem can be solved by just returning s without further recursion!
Here is a brief example of the recursion approach.
img
Algorithm
Iterate over the input string s and check if a pair exists. - If we find one pair, remove it from s, and start over this step with the remaining string. - Otherwise, return s
The trick is that each time a recursive function calls itself, it reduces the given problem into subproblems. The recursion call continues until it reaches a point where the subproblem can be solved without further recursion.
In this problem, once we find a pair that should be deleted, we are actually reducing s into a new string s' which is 2 characters smaller. Then the function calls itself for this smaller subproblem. When we can't find a pair for s, we have reached the base case where the problem can be solved by just returning s without further recursion!
Here is a brief example of the recursion approach.
img
Algorithm
Iterate over the input string s and check if a pair exists. - If we find one pair, remove it from s, and start over this step with the remaining string. - Otherwise, return s
Hey guy's hope till now every file went good getting some error while uploading files
Donβt Just LeetCode, Follow the Coding Patterns Instead.π’βοΈπ―
Everyone who is preparing DSA is well known about the platform called LeetCode.
But the main problem is - It has a massive amount of questions to practice around 2463 till date.
And if you have limited time for preparation, it would be difficult to solve these many problems.
Not because you will not be able to solve it, but because it needs a lot of time to solve and no one really likes spending that much time.
Bhaiya, but solution kya hai iska ?
Simple β Try to see Coding Problem Patterns
People used to be in the same loop by solving questions of the same data structure, due to which they might left grasping the command on a lot of important Data Structure and Algorithms.
I have gathered around 20 of these Coding problem patterns that I believe can help anyone learn these beautiful algorithmic techniques and make a real difference in the coding interviews.
1) Sliding Window
2) Islands (Matrix Traversal)
3) Two Pointers
4) Fast & Slow Pointers
5) Merge Intervals
6) Cyclic Sort
7) In-place Reversal of a LinkedList
8) Tree Breadth-First Search
9) Tree Depth First Search
10) Two Heaps
11) Subsets
12) Modified Binary Search
13) Bitwise XOR
14) Top βKβ Elements
15) K-way Merge
16) Topological Sort
17) 0/1 Knapsack
18) Fibonacci Numbers
19) Palindromic Subsequence
20) Longest Common Substring
Everyone who is preparing DSA is well known about the platform called LeetCode.
But the main problem is - It has a massive amount of questions to practice around 2463 till date.
And if you have limited time for preparation, it would be difficult to solve these many problems.
Not because you will not be able to solve it, but because it needs a lot of time to solve and no one really likes spending that much time.
Bhaiya, but solution kya hai iska ?
Simple β Try to see Coding Problem Patterns
People used to be in the same loop by solving questions of the same data structure, due to which they might left grasping the command on a lot of important Data Structure and Algorithms.
I have gathered around 20 of these Coding problem patterns that I believe can help anyone learn these beautiful algorithmic techniques and make a real difference in the coding interviews.
1) Sliding Window
2) Islands (Matrix Traversal)
3) Two Pointers
4) Fast & Slow Pointers
5) Merge Intervals
6) Cyclic Sort
7) In-place Reversal of a LinkedList
8) Tree Breadth-First Search
9) Tree Depth First Search
10) Two Heaps
11) Subsets
12) Modified Binary Search
13) Bitwise XOR
14) Top βKβ Elements
15) K-way Merge
16) Topological Sort
17) 0/1 Knapsack
18) Fibonacci Numbers
19) Palindromic Subsequence
20) Longest Common Substring