0% found this document useful (0 votes)
35 views9 pages

21BCS1130 - Abhinav Soni Day 6

Uploaded by

Abhinav Soni
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)
35 views9 pages

21BCS1130 - Abhinav Soni Day 6

Uploaded by

Abhinav Soni
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/ 9

DAY 6 Program

Student Name: Abhinav Soni UID: 21BCS1130


Branch: CSE Section/Group: 622/A
Semester: 6th Date of Performance: 01/07/2024
Subject Name: JAVA

1. Problem 1:
Aim:
Isomorphic Strings

Code:
class Solu on { public boolean isIsomorphic(String
s, String t) { int[] indexS
= new int[200]; int[] indexT = new int[200];

int len = s.length();

if(len != t.length()) { return


false;
}

for(int i = 0; i < len; i++) {


if(indexS[s.charAt(i)] != indexT[t.charAt(i)]) { return
false;
}

indexS[s.charAt(i)] = i + 1;
indexT[t.charAt(i)] = i + 1;
}

return true;
}
}

Output
Problem 2: Aim: nthFibonacci

Code:
import java.io.*; import
java.u l.*;

// Read only region start class UserMainCode


{ public long nthFibonacci(int input1){
// Read only region end // Write code
here...

if (input1 <= 0) {
return -1; // Invalid input }
else if (input1 == 1) {
return 0; // First Fibonacci number is 0
} else if (input1 == 2) { return 1; //
Second Fibonacci number is 1
} else {
long a = 0; long
b=
1; long result =
0;

for (int i = 3; i <= input1; i++) {


result = a + b; a = b; b=
result;
}

return result;
}
}
}

Output

Problem 3:
Aim:
Longest Common Prefix

Code:
class Solu on {
public String longestCommonPrefix(String[] v) {
StringBuilder ans = new StringBuilder();
Arrays.sort(v);
String first
= v[0];
String last =
v[v.length-1];
for (int i=0;
i<Math.min(first.length(), last.length()); i++) {
if (first.charAt(i) != last.charAt(i)) { return ans.toString();
}
ans.append(first.charAt(i));
}
return ans.toString();
}
}

Output

Problem 4: Aim:
Count and Say

Code:
class Solu on { public String countAndSay(int
n) { if(n==1) return "1";

String s=countAndSay(n-1);

int c=0;
StringBuilder ans=new StringBuilder();
for(int i=0;i<s.length();i++){ c++;
if(i==s.length()-1 || s.charAt(i)!=s.charAt(i+1)){
ans.append(c).append(s.charAt(i));
c=0; }
}
return ans.toString();

}
}

Output

Problem 5:
Aim:
Java Excep on Handling

Code:
import java.u l.Scanner; class MyCalculator { long power(int n,int p)
throws Excep on{ if(n<0 || p<0) throw new Excep on("n or p should not
be nega ve."); if(n==0 && p==0) throw new Excep on("n and p should not
be zero."); return (long)Math.pow(n,p);

}
}
public class Solu on { public sta c final MyCalculator my_calculator =
new MyCalculator(); public sta c final Scanner in = new
Scanner(System.in);

public sta c void main(String[] args) {


while (in .hasNextInt()) { int n = in
.nextInt(); int p = in .nextInt();
try {

System.out.println(my_calculator.power(n, p));
} catch (Excep on e) {
System.out.println(e);
}
}
}
}

Output

Problem 6:
Aim:
Container With Most Water

Code:
public class Solu on { public int
maxArea(int[] height) { int maxArea
= 0; int le = 0; int right =
height.length - 1;

while (le < right) { maxArea = Math.max(maxArea, (right - le )


* Math.min(height[le ], height[right]));
if (height[le ] < height[right]) { le ++;
} else { right--;
}
}

return maxArea;
}
}

Output

Problem 7: Aim:
Group Anagrams

Code:
class Solu on {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();

for (String word : strs) { char[] chars =


word.toCharArray();
Arrays.sort(chars);
String sortedWord = new String(chars);
if

(!map.containsKey(sortedWord)) { map.put(sortedWord, new


ArrayList<>());
}

map.get(sortedWord).add(word);
}

return new ArrayList<>(map.values());


}
}

Output

Problem 8: Aim:
Word Frequency

Code:
import java.io.File; import
java.io.FileNotFoundExcep on; import java.u l.*; public
class
WordFrequencyCounter { public sta c void
main(String[] args) {
Map<String, Integer> wordFrequency = new HashMap<>();
try {
File file = new File("words.txt"); Scanner scanner
= new Scanner(file);
while (scanner.hasNext()) {
String word = scanner.next().toLowerCase();
wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
}

List<Map.Entry<String, Integer>> sortedList = new


ArrayList<>(wordFrequency.entrySet());
sortedList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));

for (Map.Entry<String, Integer> entry : sortedList) {


System.out.println(entry.getKey() + " " + entry.getValue());
}

scanner.close();
} catch (FileNotFoundExcep on e) {
System.out.println("File not found.");
}
}
}

Output

You might also like