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

Java Fast

Java Fast learner Worksheet

Uploaded by

ritikrk008
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)
23 views

Java Fast

Java Fast learner Worksheet

Uploaded by

ritikrk008
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/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Fast Learner
Assignment: 3.2

Student Name: Ritik Kumar UID:21BCS1054


Branch: BE - CSE Section/Group: 614 - B
Semester: 6th
Subject Name: Java Project Lab Subject Code: 21CSH-319

QUESTION 1. String t is generated by random shuffling string s and then add one more letter at a
random position. Return the letter that was added to t.
Hint: Input: s = "abcd", t = "abcde" Output: "e".

import java.util.Scanner; public


class FindAddedLetter {
public static char findAddedLetter(String s, String t) {int[]
count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
for (char c : t.toCharArray()) {
count[c - 'a']--;
}
for (int i = 0; i < 26; i++) {if
(count[i] < 0) {
return (char) ('a' + i);
}
}
return ' ';
}
public static void main(String[] args) { Scanner
scanner = new Scanner(System.in);
System.out.println("Enter string s:");
String s = scanner.nextLine();
System.out.println("Enter string t:");
String t = scanner.nextLine();
char addedLetter = findAddedLetter(s, t);
System.out.println("Added letter: " + addedLetter);
scanner.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
QUESTION 2. A string containing only parentheses is balanced if the following is true: 1. if it is an empty
string 2. if A and B are correct, AB is correct, 3. if A is correct, (A) and {A} and [A] are also correct.
Examples of some correctly balanced strings are: "{}()", "[{()}]", "({()})" Examples of some unbalanced
strings are: "{}(", "({)}", "[[", "}{" etc. Given a string, determine if it is balanced or not.

import java.util.Scanner;
import java.util.Stack;
public class BalancedParentheses {
public static boolean isBalanced(String str) {
Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) {
return false;
}
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string containing only parentheses:");
String input = scanner.nextLine();
boolean balanced = isBalanced(input);
if (balanced) {
System.out.println("The string is balanced.");
} else {
System.out.println("The string is not balanced.");
}
scanner.close();
}
}

QUESTION 3. Comparators are used to compare two objects. In this challenge, you'll create a comparator
and use it to sort an array. The Player class has fields: a String and a integer. Given an array of Player
objects, write a comparator that sorts them in order of decreasing score; if or more players have the same
score, sort those players alphabetically by name. To do this, you must create a Checker class that
implements the Comparator interface, then write an int compare(Player a, Player b) method implementing
the Comparator.compare(T o1, T o2) method.
import java.util.*;
class Player {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
String name;
int score;
Player(String name, int score) {
this.name = name;
this.score = score;
}
}
class Checker implements Comparator<Player> {
public int compare(Player a, Player b) {
if (a.score != b.score) {
return Integer.compare(b.score, a.score);
} else {
return a.name.compareTo(b.name);
}
}
}
public class PlayerComparator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of players:");
int n = scanner.nextInt();
scanner.nextLine();
List<Player> players = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.println("Enter player name and score separated by space:");
String[] input = scanner.nextLine().split(" ");
String name = input[0];
int score = Integer.parseInt(input[1]);
players.add(new Player(name, score));
}
Collections.sort(players, new Checker());
for (Player player : players) {
System.out.println(player.name + " " + player.score);
}
scanner.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
QUESTION 4. Given an input string (s) and a pattern (p), implement wildcard pattern matching with
support for '?' and '*' where: • '?' Matches any single character. • '*' Matches any sequence of characters
(including the empty sequence). The matching should cover the entire input string (not partial).

import java.util.Scanner;
public class WildcardMatching {
public static boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') {
dp[0][j] = dp[0][j - 1];
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '?' || s.charAt(i - 1) == p.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
}
}
}
return dp[m][n];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the input string (s):");
String s = scanner.nextLine();
System.out.println("Enter the pattern string (p):");
String p = scanner.nextLine();
boolean result = isMatch(s, p);
System.out.println("Output: " + result);
scanner.close();
}
}

QUESTION 5. Given an array of integers nums sorted in non-decreasing order, find the starting and
endingposition of a given target value. If target is not found in the array, return [-1, -1]. You must write an
algorithm with O(log n) runtime complexity.

import java.util.*;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public class FindTargetRange {
public static int[] searchRange(int[] nums, int target) {
int[] result = {-1, -1};
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (left < nums.length && nums[left] == target) {
result[0] = left;
} else {
return result;
}
right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
result[1] = right
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the elements of the array :");
String[] input = scanner.nextLine().split(" ");
int[] nums = new int[input.length];
for (int i = 0; i < input.length; i++) {
nums[i] = Integer.parseInt(input[i]);
}
System.out.println("Enter the target value:");
int target = scanner.nextInt();
int[] result = searchRange(nums, target);
System.out.println("Output: [" + result[0] + ", " + result[1] + "]");
scanner.close();
}
}

You might also like