diff --git a/GeeksForGeeks/25-1-24/GFG.java b/GeeksForGeeks/25-1-24/GFG.java new file mode 100644 index 0000000..b13f6aa --- /dev/null +++ b/GeeksForGeeks/25-1-24/GFG.java @@ -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 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 diff --git a/GeeksForGeeks/25-1-24/README.md b/GeeksForGeeks/25-1-24/README.md new file mode 100644 index 0000000..b0ea7a8 --- /dev/null +++ b/GeeksForGeeks/25-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(n) diff --git a/LeetCode/25-1-24/README.md b/LeetCode/25-1-24/README.md new file mode 100644 index 0000000..d345d8d --- /dev/null +++ b/LeetCode/25-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(m*n) +Space complexity - O(m*n) diff --git a/LeetCode/25-1-24/Solution.java b/LeetCode/25-1-24/Solution.java new file mode 100644 index 0000000..98a17c6 --- /dev/null +++ b/LeetCode/25-1-24/Solution.java @@ -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]; + } +}