0% found this document useful (0 votes)
12 views4 pages

21BCS1704 Tushar 9

Uploaded by

tushar0chauhan
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)
12 views4 pages

21BCS1704 Tushar 9

Uploaded by

tushar0chauhan
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/ 4

ASSIGNMENT 1

Java

Name: Tushar UID: 21BCS1704


Branch: BE CSE Section/Group: 21BCS_CC-607A
Semester: 6 Date of Performance: 26/06/ 24

1. Java String Reverse - HACKERRANK

import java.util.Scanner;
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
scanner.close();
}
}
2. Count primes – Hackerrank
class Solution {
public int countPrimes(int n) {
if (n <= 2)
return 0;
final boolean[] isPrime = sieveEratosthenes(n);
int ans = 0;
return (int) IntStream.range(0, isPrime.length)
.mapToObj(i -> isPrime[i])
.filter(p -> p)
.count();
}
private boolean[] sieveEratosthenes(int n) {
boolean[] isPrime = new boolean[n];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < n; ++i)
if (isPrime[i])
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
return isPrime;
}
}

3. Find the index of the first occurrence in a string – leetcode


class Solution {
public int strStr(String haystack, String needle) {
final int m = haystack.length();
final int n = needle.length();

for (int i = 0; i < m - n + 1; ++i)


if (haystack.substring(i, i + n).equals(needle))
return i;

return -1;
}
}
4. move zeroes -leetcode
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;
}
}
5. Delete leaves with a given value – leetcode
class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {
if (root == null)
return null;
root.left = removeLeafNodes(root.left, target);
root.right = removeLeafNodes(root.right, target);
return isLeaf(root) && root.val == target ? null : root;
}

private boolean isLeaf(TreeNode root) {


return root.left == null && root.right == null;
}
}

6. Jump game II – leetcode


class Solution {
public int jump(int[] nums) {
int ans = 0;
int end = 0;
int farthest = 0;

// Start an implicit BFS.


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) { // Visited all the items on the current level.
++ans; // Increment the level.
end = farthest; // Make the queue size for the next level.
}
}

return ans;
}
}
7. Reverse Words in a String – leetcode
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder(s).reverse(); // Reverse the whole string.
reverseWords(sb, sb.length()); // Reverse each word.
return cleanSpaces(sb, sb.length()); // Clean up the spaces.
}

private void reverseWords(StringBuilder sb, int n) {


int i = 0;
int j = 0;

while (i < n) {
while (i < j || i < n && sb.charAt(i) == ' ') // Skip the spaces.
++i;
while (j < i || j < n && sb.charAt(j) != ' ') // Skip the spaces.
++j;
reverse(sb, i, j - 1); // Reverse the word.
}
}

// Trim leading, trailing, and middle spaces


private String cleanSpaces(StringBuilder sb, int n) {
int i = 0;
int j = 0;
while (j < n) {
while (j < n && sb.charAt(j) == ' ') // Skip the spaces.
++j;
while (j < n && sb.charAt(j) != ' ') // Keep non spaces
sb.setCharAt(i++, sb.charAt(j++));
while (j < n && sb.charAt(j) == ' ') // Skip the spaces.
++j;
if (j < n) // Keep only one space.
sb.setCharAt(i++, ' ');
}

return sb.substring(0, i).toString();


}

private void reverse(StringBuilder sb, int l, int r) {


while (l < r) {
final char temp = sb.charAt(l);
sb.setCharAt(l++, sb.charAt(r));
sb.setCharAt(r--, temp);
}
}
}
8. Unique digits count - Metll Test Authenticate
class Solution {
public int strStr(String haystack, String needle) {
final int m = haystack.length();
final int n = needle.length();

for (int i = 0; i < m - n + 1; ++i)


if (haystack.substring(i, i + n).equals(needle))
return i;

return -1;
}
}

You might also like