Skip to content

Commit

Permalink
Merge pull request #95 from sanskar28/master
Browse files Browse the repository at this point in the history
Updated Common Child in Java
  • Loading branch information
maze-runnar authored Dec 31, 2019
2 parents f3cb8a5 + fc5c643 commit b4725e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ in C++, Java, Python and Javascript.
||[Making Anagrams](https://www.hackerrank.com/challenges/ctci-making-anagrams/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/making_anagrams.cpp)|[Done](/String%20Manipulation/MakingAnagrams.java)|[Done](/String%20Manipulation/MakingAnagrams.py)||
||[Sherlock and Valid String](https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/sherlock_and_valid_string.cpp)||[Done](/String%20Manipulation/SherlockValidString.py)||
||[Special String Again](https://www.hackerrank.com/challenges/special-palindrome-again/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/special_palindrome_again.cpp)||[Done](/String%20Manipulation/SpecialStringAgain.py)||
||[Common Child](https://www.hackerrank.com/challenges/common-child/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/common_child.cpp)||[Done](/String%20Manipulation/CommonChild.py)||
||[Common Child](https://www.hackerrank.com/challenges/common-child/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/common_child.cpp)|[Done](/String%20Manipulation/CommonChild.java)|[Done](/String%20Manipulation/CommonChild.py)||
||[Alternating Characters](https://www.hackerrank.com/challenges/alternating-characters/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/String%20Manipulation/alternating_characters.cpp)|[Done](/String%20Manipulation/alternating_characters.java)|[Done](/String%20Manipulation/AlternatingCharacters.py)||
|Greedy Algorithms||||||
| |[Luck Balance](https://www.hackerrank.com/challenges/luck-balance/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/luck_balance.cpp)||[Done](/Greedy%20Algorithm/LuckBalance.py)||
Expand Down
39 changes: 39 additions & 0 deletions String Manipulation/CommonChild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
String s2=br.readLine();
int M = s.length();
int N = s2.length();

int[][] arr = new int[M+1][N+1];
for (int i = M-1; i >= 0; i--) {
for (int j = N-1; j >= 0; j--) {
if (s.charAt(i) == s2.charAt(j))
arr[i][j] = arr[i+1][j+1] + 1;
else
arr[i][j] = Math.max(arr[i+1][j], arr[i][j+1]);
}
}
int i = 0, j = 0;
int count =0;
while(i < M && j < N) {
if (s.charAt(i) == s2.charAt(j)) {
count++;
i++;
j++;
}
else if (arr[i+1][j] >= arr[i][j+1]) i++;
else j++;
}
System.out.println(count);
}
}

0 comments on commit b4725e2

Please sign in to comment.