Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Winning Camp Worksheet (Java)


Day: 18
Name : Saurav Singha UID : 21BCS5421
Subject : Java Section : SC-904 (B)
Date : 15/06/2024

Problem 1: Remove K digit


Solution:
class Solution {
public String removeKdigits(String num, int k) {
Deque<Character> stack = new LinkedList<>();

for (Character c : num.toCharArray()) {


while (!stack.isEmpty() && stack.peek() > c && k > 0) {
stack.pop();
k--;
}
stack.push(c);
}

// Remove leading zeros


while (!stack.isEmpty() && stack.peekLast() == '0') {
stack.pollLast();
}

// Remove remaining digits if k > 0


while (!stack.isEmpty() && k > 0) {
stack.pop();
k--;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

// Construct the result from the stack


StringBuilder result = new StringBuilder();
while (!stack.isEmpty()) {
result.append(stack.pollLast());
}
return result.isEmpty() ? "0" : result.toString();
}
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2: String Compression


Solution:
class Solution {
public int compress(char[] chars) {
int i = 1;
int ans = 0;
int count = 1;
char v = chars[0];
while (i < chars.length) {
if (v == chars[i]) {
count++;
} else {
chars[ans++] = chars[i - 1];
if (count > 1) {
String str = String.valueOf(count);
for (char digit : str.toCharArray()) {
chars[ans++] = digit;
}
}
v = chars[i];
count = 1;
}
i++;
}
chars[ans++] = chars[i - 1];
if (count > 1) {
String str = String.valueOf(count);
for (char digit : str.toCharArray()) {
chars[ans++] = digit;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return ans;
}
}

Output:

Problem 3: Occurrence after bigram


Solution:
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] st = text.split(" ");
List<String> l = new ArrayList<String>();
int i = 0, n = st.length;
while (i < n) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (st[i].equals(first)) {
if (i + 1 < n - 1 && st[i + 1].equals(second)) {
l.add(st[i + 2]);
}
}
i++;
}
return l.toArray(new String[0]);
}
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 4: Trapping rain water


Solution:
class Solution {
public int trap(int[] height) {
int i = 0, left_max = height[0], sum = 0;
int j = height.length - 1, right_max = height[j];
while (i < j) {
if (left_max <= right_max) {
sum += (left_max - height[i]);
i++;
left_max = Math.max(left_max, height[i]);
} else {
sum += (right_max - height[j]);
j--;
right_max = Math.max(right_max, height[j]);
}}
return sum;
}
}
Output:

You might also like