DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Worksheet Day 6
Student Name: Priyanshu Malik UID: 21BCS7586
Branch: CSE Section/Group: 21BCS_CC-652-A
Subject Name: Java Date of Performance: 03/06/24
Aim:
To demonstrate the concept of programming algorithms.
Objective:
1) Isomorphic Strings - Given two strings s and t, determine if they are isomorphic. Two strings
s and t are isomorphic if the characters in s can be replaced to get t. (Level – Medium)
2) Longest Common Prefix - Write a function to find the longest common prefix string amongst
an array of strings. If there is no common prefix, return an empty string "". (Level – Medium)
3) Java Exception Handling - You are required to compute the power of a number by
implementing a calculator. Create a class MyCalculator which consists of a single method long
power(int, int). (Level – Medium)
4) Metll Test Authenticate - (Level – Medium)
Code:
1)
class Solution { public boolean
isIsomorphic(String s, String t) { int
map1[]=new int[200]; int map2[]=new int[200];
if(s.length()!=t.length()) return false;
for(int i=0;i<s.length();i++)
{ //Yuvraj Singh
if(map1[s.charAt(i)]!=map2[t.charAt(i)])
return false; map1[s.charAt(i)]=i+1;
map2[t.charAt(i)]=i+1;
}
return true;
} }
2)
class Solution {
public String longestCommonPrefix(String[] strs) {
Arrays.sort(strs);
String s1 = strs[0];
String s2 = strs[strs.length-1]; int
idx = 0; while(idx < s1.length() && idx <
s2.length()){ if(s1.charAt(idx) ==
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
s2.charAt(idx)){ idx++; }
else { break;
} }
return s1.substring(0, idx);
}
} 3) public long power(int n,int p) throws Exception{
if((n<0 || p<0) || (n<0 && p<0)){ throw new
Exception("n or p should not be negative.");
} if(n==0 && p==0){ throw new
Exception("n and p should not be zero.");
} long
result=1; for(int
i=0;i<p;i++){
result*=n;
}
return result;
}
}
4)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING