Remove Duplicate Letters with Stack solution - LeetCode #316 | Python, JavaScript, Java, C++

preview_player
Показать описание
Let's solve Remove Duplicate Letters with Python, JavaScript, Java and C++ LeetCode #316! This is LeetCode daily coding challenge on September, 26th, 2023.

------------------------------------------------------------------------------------------------------
⭐️ Please subscribe to my channel from here.

⭐️ Relaxing Channel I've been working on. Please subscribe from here.

⭐️ Please upvote my article.
------------------------------------------------------------------------------------------------------

■ Timeline
0:00 Read the question of Remove Duplicate Letters
0:16 How we think about a solution - What is the smallest in lexicographical order for this question?
2:28 Demonstrate solution with an example.
7:43 Coding
10:00 Time Complexity and Space Complexity

■ Blind 75 LeetCode questions

■ Twitter

■ Instagram

■ Problem Link

■ Codes in the video
- Python

- JavaScript

- Java

- C++

■ Playlists

#pythonprogramming #leetcode #algorithm #coding #programming #python #javascript #java #cplusplus
Рекомендации по теме
Комментарии
Автор

Regarding Space complexity O(n), where n is length of input "s". n is also O(26) at most because we are not allowed to have duplicate characters, so we can say O(1) instead of O(n).

CodingNinjaExAmazon
Автор

class solution {
public string remove Duplicate Letters (string s){
stack <interger >st=new stack <>();
int[]last=new int [26], taken=new int [26];
for(int for(inti=0;i<s.length;i++){ int c=s.charAt (i);
if(taken [c]++>0) continue;
While (!st.is Empty ()&&st.peek ()>c&&i<last [st.peek ()]);
taken [st.pop ()]=0;
st.push (c);
}
string Builder sb=new string Builder <>();
for(int i:st()) sb.append ((char)('a'+i));
return sb.to string ();
}
};

dayashankarlakhotia
join shbcf.ru