0% found this document useful (0 votes)
8 views

Day 6

yes

Uploaded by

donreturnwell
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)
8 views

Day 6

yes

Uploaded by

donreturnwell
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

DEPARTMENTOF

COMPUTERSCIENCE&ENGINEERING

Assignment 6
Student Name: Himanshu UID: 21BCS5642
Branch: CSE Section/Group: SC-904-B
Subject Name: Advance Java Date: 03/06/24

Ques 1) Isomorphic Strings

class Solution {
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:

Ques 2): Longest Common Prefix

class Solution {
public String longestCommonPrefix(String[] v)
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
{
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:

Ques 3) Count and Say

class Solution {
public static String countAndSay(int n) {
if(n==1) return "1";
String ans=helper(countAndSay(n-1));
return ans;
}

private static String helper(String str) {


String ans="";
for(int i=str.length()-1;i>=0;i--) {
int count=1;
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
while(i>0) {
if(str.charAt(i)==str.charAt(i-1)) {
i--;
count++;
}else break;
}

ans=Integer.toString(count)+str.charAt(i)+a
ns;
}
return ans;
}

OUTPUT:

Ques 4) Container With Most Water

class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int max = 0;
while(left < right){
int w = right - left;
int h = Math.min(height[left],
height[right]);
int area = h * w;
max = Math.max(max, area);
if(height[left] < height[right]) left++;
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
else if(height[left] > height[right]) right--;
else {
left++;
right--;
}
}
return max;
}
}
OUTPUT:

You might also like