-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b85515
commit 9973774
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(m*n) | ||
Space complexity - O(m*n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |