DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Worksheet Day 2
Student Name: Priyanshu Malik UID :21BCS7586
Branch: CSE Section/Group: 21BCS_CC-652-A
Subject Name: Java Date of Performance: 27/05/24
Aim:
To demonstrate the concept of programming algorithms.
Objective:
1) Java String Reverse - A palindrome is a word, phrase, number, or other sequence of characters
which reads the same backward or forward. Given a string A, print Yes if it is a palindrome,
print No otherwise. (Level – Easy)
2) Count Primes - Given an integer n, return the number of prime numbers that are strictly less
than n. (Level – Medium)
3) Find the Index of the First Occurrence in a String - Given two strings needle and haystack,
return the index of the first occurrence of needle in haystack, or -1 if needle is not part of
haystack. (Level – Easy)
4) Delete Leaves With a Given Value - Given a binary tree root and an integer target, delete all
the leaf nodes with value target. Note that once you delete a leaf node with value target, if its
parent node becomes a leaf node and has the value target, it should also be deleted (you need
to continue doing that until you cannot). (Level – Medium)
5) Move Zeroes - Given an integer array nums, move all 0's to the end of it while maintaining the
relative order of the non-zero elements. Note that you must do this in-place without making a
copy of the array. (Level – Medium)
6) Jump Game II - You are given a 0-indexed array of integers nums of length n. You are initially
positioned at nums[0]. Each element nums[i] represents the maximum length of a forward jump
from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where: 0
<= j <= nums[i] and i + j < n Return the minimum number of jumps to reach nums[n
- 1]. The test cases are generated such that you can reach nums[n - 1]. (Level – Hard)
7) Reverse Words in a String - Given an input string s, reverse the order of the words. A word is
defined as a sequence of non-space characters. The words in s will be separated by at least one
space. Return a string of the words in reverse order concatenated by a single space. Note that s
may contain leading or trailing spaces or multiple spaces between two words. The returned
string should only have a single space separating the words. Do not include any extra spaces.
(Level – Medium)
8) Metll Test Authenticate - (Level – Hard)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
1)
import java.io.*; import java.util.*; public
class Solution { public static void
main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
StringBuilder B=new StringBuilder();
B.append(A); B.reverse();
if(B.toString().equals(A)){
System.out.println("Yes");
} else{
System.out.println("No");
}
}
}
2)
class Solution { public static int
countPrimes(int n) { if (n <= 2) return
0;
boolean[] isPrime = new boolean[n]; for
(int i = 2; i < n; i++) { isPrime[i] =
true;
} for (int i = 2; i * i < n; i++) { if
(!isPrime[i]) continue; for (int j = i
* i; j < n; j += i) { isPrime[j] =
false;
} } int count = 0; for
(int i = 2; i < n; i++) { if
(isPrime[i]) count++;
} return
count; }
} 3) public class Solution { public int strStr(String
haystack, String needle) { int n1 = haystack.length();
int n2 = needle.length(); if (n2 > n1) { return -1;
} int j = 0; int i = 0; int start = 0; while (i <
n1 && j < n1) { if (haystack.charAt(i) ==
needle.charAt(j)) { if (i - start + 1 == n2) {
return start;
}
j++;
i++;
} else { j =
0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
start++;
i =
start;
} }
return -1;
}
} 4) class Solution { public TreeNode removeLeafNodes(TreeNode root,
int target) { if(root != null){
root.left=removeLeafNodes(root.left,target); root.right =
removeLeafNodes(root.right, target); if(root.left==null &&
root.right==null && root.val==target){ return null;
} return
root;
} return
null;
}
} 5) class
Solution {
public void
moveZeroes(int[
] nums) { int i
= 0; for (final
int num : nums)
if (num != 0)
nums[i++] =
num;
while (i < nums.length) nums[i++]
= 0;
}
} 6) class Solution { public int jump(int[] nums) { int
ans = 0; int end = 0; int farthest = 0; for (int i = 0;
i < nums.length - 1; ++i) { farthest = Math.max(farthest,
i + nums[i]); if (farthest >= nums.length - 1) {
++ans; break;
} if (i == end) {
++ans; end =
farthest;
} }
return ans;
}
} 7) class Solution { public String
reverseWords(String s) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
StringBuilder sb = new StringBuilder(s).reverse();
reverseWords(sb, sb.length()); return
cleanSpaces(sb, sb.length());
} private void reverseWords(StringBuilder sb, int n) {
int i = 0; int j = 0; while (i < n) { while (i < j ||
(i < n && sb.charAt(i) == ' '))
++i; while (j < i || (j < n &&
sb.charAt(j) != ' '))
++j; reverse(sb,
i, j - 1); }
} private String cleanSpaces(StringBuilder sb, int n)
{ int i = 0; int j = 0; while (j < n) { while (j < n
&& sb.charAt(j) == ' ')
++j; while (j < n && sb.charAt(j)
!= ' ') sb.setCharAt(i++,
sb.charAt(j++));
while (j < n && sb.charAt(j) == ' ')
++j; if (j < n)
sb.setCharAt(i++, ' ');
} return sb.substring(0,
i).toString();
} private void reverse(StringBuilder sb, int l, int r)
{ while (l < r) {//Yuvraj Singh final char temp =
sb.charAt(l); sb.setCharAt(l++, sb.charAt(r));
sb.setCharAt(r--, temp);
}
}
} 8) public class UserMainCode { public int
isMultiple(int input1, int input2) { if (input2 ==
0 || input1 == 0) { return 3;
} else if (input1 % input2 == 0) {
return 2;
} else { return
1;
}
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING