Lexicographically Smallest Subsequence- Important Java Coding Problem Solved & Explained

preview_player
Показать описание
Lexicographically Smallest Subsequence- Important Java Coding Problem Solved & Explained With Example.

If you find this video helpful, please 'Like' or 'Subscribe'.
This is really helpful for the channel and also motivates me to do more of such good videos.

Problem Description:
Return the lexicographically smallest subsequence of s of length k.

A subsequence is a string that can be derived from another string by deleting
some or no characters without changing the order of the remaining characters.

A string a is lexicographically smaller than a string b if in the first
position where a and b differ, string a has a letter that appears earlier in
the alphabet than the corresponding letter in b.

#coding #softwareengineer #Leetcode #Algorithm #DataStructure #Java #programmer #tech #software #codinglife #Preparation
#interview #programminglife #programmingisfun #hashset #atcoder #codewars #leetcode #codeforces #algorithm #algorithms #datastructure #datastructuresandalgorithms #programming #computerscience
#interviewpreparation #codingquestions #codinginterviewquestions #array #arrays #matrices #matrix #bfs #dfs
Рекомендации по теме
Комментарии
Автор

thank u for the insides. it helped me alot

pranaymore
Автор

public String solve(String A) {
Stack<Character> stack = new Stack<>();
int N = A.length();

for(int i =0; i< N; i++){
if(stack.isEmpty()){
stack.add(A.charAt(i));
}
else{
while(!stack.isEmpty() && (A.charAt(i) < stack.peek()) && (stack.size()+N-i-1 >=2)){
stack.pop();
}

if(stack.isEmpty() || stack.size()<2){
stack.add(A.charAt(i));
}
}
}

StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
sb.append(stack.peek());
stack.pop();
}
return sb.reverse().toString();


}

pranaymore
welcome to shbcf.ru