Remove Duplicate Letters | Leetcode

  Рет қаралды 24,405

Techdose

Techdose

2 жыл бұрын

This video explains a very important programming interview problem which is based on a monotonic stack. I have explained the problem statement with intuition for why should we maintain an increasing order of the elements and then I have also explained why do we need other data structures like frequency array and a visited array.
CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
======================================PLEASE DONATE=============================
🧡 SUPPORT OUR WORK: / techdose
💚 UPI-ID: surya.kahar@ybl
💞JOIN Membership: / @techdose4u
==============================================================================
INSTAGRAM : / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
=======================================================================
USEFUL LINKS:
🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
🟡Get your dream job in 1 month: • 🔴Get your dream job in...
🔵How to crack dream job in just 2 months: • How to crack dream job...
🟣7 Days DSA plan: techdose.co.in/7-days-dsa-che...
RELATED LINKS:
Trapping Rainwater Problem: • Trapping Rainwater Pro...
CODE LINK: gist.github.com/SuryaPratapK/...

Пікірлер: 34
@techdose4u
@techdose4u 8 ай бұрын
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037 🔴 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/
@MP-ny3ep
@MP-ny3ep 9 ай бұрын
The way you explained why we need a frequency set and a visited set is just phenomenal!
@AjeetKumar-tt1jr
@AjeetKumar-tt1jr 9 ай бұрын
I loved the way you approached the problem, just awesome... thanks
@pritz9
@pritz9 9 ай бұрын
Clear And Concise!
@vineetsharma2604
@vineetsharma2604 Жыл бұрын
Great explaination, thanks a lot!
@AnkitKumar-ys7vs
@AnkitKumar-ys7vs 9 ай бұрын
Thaks way of explanination is very good.
@pryansh_
@pryansh_ 9 ай бұрын
thanks ! it was helpful.
@thecricketerraja
@thecricketerraja Жыл бұрын
really very nicely explained , big thank you 🙏❤
@techdose4u
@techdose4u Жыл бұрын
My pleasure 😊
@ihsannuruliman3656
@ihsannuruliman3656 2 жыл бұрын
Hi, could you make a video on Optimal Binary Search Tree? I've watched couple of videos on KZbin but it's still doesn't make sense to me. I think you're the best in terms of explaining.
@chandrashekharagrawal4890
@chandrashekharagrawal4890 9 ай бұрын
Amazing explanation!!
@rbk.technology4747
@rbk.technology4747 Жыл бұрын
Perfect perfect video. Understood.
@maruthkamath9304
@maruthkamath9304 2 жыл бұрын
How can the time complexity in this case be said to be just O(N) , when you may have to pop all elements of the stack for just placing 1 element. Would the pop operations not add up to the complexity?
@techdose4u
@techdose4u 2 жыл бұрын
One letter can only be pushed and popped once. So, total push + pop can be 2N :)
@agnesakne4409
@agnesakne4409 11 ай бұрын
Great explanation.
@satyamsrivastava2440
@satyamsrivastava2440 9 ай бұрын
phenomenal explanation
@FullMetalAlgorithmist
@FullMetalAlgorithmist 9 ай бұрын
well explained.
@pabitrakb5291
@pabitrakb5291 9 ай бұрын
Nice explanation
@HarishKumar-bx8ht
@HarishKumar-bx8ht Жыл бұрын
Hee once you have frequency array .. you can just traverse the freq array and check freq >0 then append to answer . Which will be more efficient right ?
@adee6467
@adee6467 Жыл бұрын
Order won't be maintained
@yaswanthp2294
@yaswanthp2294 Жыл бұрын
Lovely
@houssemelhadj6881
@houssemelhadj6881 2 жыл бұрын
Thank for your work, doese your code actually at the end validate, (did you submit it ) it seems to me there maybe something missing. at the end you returned "bca" as answer and i thought the answer should be "abc". or do i need just to return the smallest substring, so that the order of them is given by the original string? for example: bacbab -> acb or abc ? anyway Thanks for your good work
@techdose4u
@techdose4u 2 жыл бұрын
You need to return the smallest unique lettered subsequence. So answer will be acb.
@houssemelhadj6881
@houssemelhadj6881 2 жыл бұрын
@@techdose4u yes yes i first missunderstood the problem, and finally did it myself in python ( around 90 % acceptance ) inspired by your method. I didn't use the visited list thought. I directly checked the stack if it holds the element before pushing or popping. and it worked blissfully :) very thanks
@pankajmaurya64
@pankajmaurya64 2 жыл бұрын
@@techdose4u After removing duplicate element from input "bcab", output will be "bca" -> according to leetcode
@fazilshafi8083
@fazilshafi8083 2 ай бұрын
Java Solution -----> public class Solution { public String removeDuplicateLetters(String s) { Map freq = new HashMap(); for (int i = 0; i < s.length(); ++i){ char ch = s.charAt(i); freq.put(ch, freq.getOrDefault(ch, 0) + 1); } // Monotonic stack to maintain increasing order of chars Stack stack = new Stack(); Map seen = new HashMap(); // Track already included elements for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); if (seen.containsKey(s.charAt(i))) { // Don't process already included char freq.put(ch, freq.get(ch) - 1); continue; } while (!stack.isEmpty() && stack.peek() > ch && freq.get(stack.peek()) > 0) { // Pop all possible larger chars seen.remove(stack.peek()); stack.pop(); } stack.push(ch); seen.put(ch, true); freq.put(ch, freq.get(ch) - 1); } StringBuilder ans = new StringBuilder(); while (!stack.isEmpty()) { ans.append(stack.pop()); } return ans.reverse().toString(); } }
@yashagrawal1663
@yashagrawal1663 Жыл бұрын
is there a way to to solve this in linear space without taking a stack data structure
@priyanshumaikhuri5856
@priyanshumaikhuri5856 11 ай бұрын
Yes, you could use string instead of stack
@yashgoswami5374
@yashgoswami5374 2 жыл бұрын
Can't we just put all chars in set and get unique, isn't it that simple
@techdose4u
@techdose4u 2 жыл бұрын
Need to maintain order of elements as mentioned in string :)
@pankajmaurya64
@pankajmaurya64 2 жыл бұрын
@TECH DOSE After removing duplicate element from input "bcab", output will be "bca" -> according to leetcode
@anonymous-sk2pr
@anonymous-sk2pr 2 жыл бұрын
1st 😉😉😉
@QuynhNhu-gu5ql
@QuynhNhu-gu5ql 2 күн бұрын
hay chi chi rô
@who_thekaushik
@who_thekaushik Жыл бұрын
bhai aisi vids bna ne se accha toh mat he bna
REMOVE DUPLICATE LETTERS | LEETCODE 316 | PYTHON STACK SOLUTION
18:15
Cracking FAANG
Рет қаралды 1,4 М.
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 86 М.
когда повзрослела // EVA mash
00:40
EVA mash
Рет қаралды 1,1 МЛН
Luck Decides My Future Again 🍀🍀🍀 #katebrush #shorts
00:19
Kate Brush
Рет қаралды 8 МЛН
Shortest Subarray with Sum at Least K | Leetcode #862
21:31
Techdose
Рет қаралды 31 М.
Remove Duplicate Letters | Leetcode 316
33:47
Pepcoding
Рет қаралды 21 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 749 М.
Leetcode - Remove Duplicate Letters (Python)
6:31
Timothy H Chang
Рет қаралды 4,7 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 606 М.
Find Median from Data Stream
29:28
Techdose
Рет қаралды 53 М.