Skip to content

Commit

Permalink
Added codes for 25 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 25, 2024
1 parent 5b85515 commit 9973774
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
103 changes: 103 additions & 0 deletions GeeksForGeeks/25-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;

// } Driver Code Ends
//User function Template for Java


class Pair
{
int first;
int second;
Pair(int first,int second)
{
this.first=first;
this.second=second;
}
}

class Solution
{
int solve(int num1,int num2)
{
//code here
if(num1==num2)
return 0;

boolean []prime=new boolean[10000];
Arrays.fill(prime,true);

prime[1]=false;

for(int i=2;i<=9999;i++)
{
if(prime[i])
{
for(int j=i*i;j<=9999;j+=i)
{
prime[j]=false;
}
}
}

Queue<Pair> queue=new LinkedList<>();
queue.add(new Pair(num1,0));

boolean []vis=new boolean[10000];

while(!queue.isEmpty())
{
int num=queue.peek().first;
int step=queue.peek().second;
queue.poll();

if(num==num2)
return step;

if(vis[num])
continue;

vis[num]=true;

String str=Integer.toString(num);

for(int i=0;i<4;i++)
{
for(char j='0';j<='9';j++)
{
if((i==0 && j=='0') || j==str.charAt(i))
continue;
else
{
String newStr=str.substring(0,i)+j+str.substring(i+1);
int val=Integer.valueOf(newStr);
if(prime[val])
queue.add(new Pair(val,step+1));
}
}
}
}
return -1;
}
}


//{ Driver Code Starts.
class GFG{
public static void main(String args[]) throws IOException{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String input_line[] = read.readLine().trim().split("\\s+");
int Num1=Integer.parseInt(input_line[0]);
int Num2=Integer.parseInt(input_line[1]);

Solution ob = new Solution();
System.out.println(ob.solve(Num1,Num2));
}
}
}
// } Driver Code Ends
2 changes: 2 additions & 0 deletions GeeksForGeeks/25-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/25-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n)
Space complexity - O(m*n)
18 changes: 18 additions & 0 deletions LeetCode/25-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution
{
public int longestCommonSubsequence(String text1, String text2)
{
final int m = text1.length();
final int n = text2.length();

int[][] dp = new int[m + 1][n + 1];

for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
dp[i + 1][j + 1] = text1.charAt(i) == text2.charAt(j)
? 1 + dp[i][j]
: Math.max(dp[i][j + 1], dp[i + 1][j]);

return dp[m][n];
}
}

0 comments on commit 9973774

Please sign in to comment.