diff --git a/README.md b/README.md index 7a06daa..c22885d 100644 --- a/README.md +++ b/README.md @@ -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)|| diff --git a/String Manipulation/CommonChild.java b/String Manipulation/CommonChild.java new file mode 100644 index 0000000..9220572 --- /dev/null +++ b/String Manipulation/CommonChild.java @@ -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); + } +}