Skip to content

Commit c941169

Browse files
Accepted Solutions
1 parent 3030ca1 commit c941169

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ My accepted leetcode solutions to some of the common interview problems.
249249
- [One Edit Distance](problems/src/string/OneEditDistance.java) (Medium)
250250
- [Count and Say](problems/src/string/CountAndSay.java) (Easy)
251251
- [Multiply Strings](problems/src/string/MultiplyStrings.java) (Medium)
252+
- [Longest Word in Dictionary through Deleting](problems/src/string/LongestWordInDictonary.java) (Medium)
252253

253254
#### [Tree](problems/src/tree)
254255

@@ -285,6 +286,7 @@ My accepted leetcode solutions to some of the common interview problems.
285286
- [Convert Binary Search Tree to Sorted Doubly Linked List](problems/src/tree/BSTtoDoublyLinkedList.java) (Easy)
286287
- [Same Tree](problems/src/tree/SameTree.java) (Easy)
287288
- [Binary Tree Longest Consecutive Sequence II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
289+
- [Minimum Absolute Difference in BST](problems/src/tree/MinimumAbsoluteDifferenceInBST.java) (Medium)
288290

289291
#### [Two Pointers](problems/src/two_pointers)
290292

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package string;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 15/02/2018.
7+
* Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting
8+
* some characters of the given string. If there are more than one possible results, return the longest word with the
9+
* smallest lexicographical order. If there is no possible result, return the empty string.
10+
11+
Example 1:
12+
Input:
13+
s = "abpcplea", d = ["ale","apple","monkey","plea"]
14+
15+
Output:
16+
"apple"
17+
Example 2:
18+
Input:
19+
s = "abpcplea", d = ["a","b","c"]
20+
21+
Output:
22+
"a"
23+
Note:
24+
All the strings in the input will only contain lower-case letters.
25+
The size of the dictionary won't exceed 1,000.
26+
The length of all the strings in the input won't exceed 1,000.
27+
28+
Solution: O((n x m x log n) + (m ^ 2 + m x n))) sort the dictionary based on the longest first and then
29+
lexicographically and compare each sorted word with given word and do a two pointer comparison to check for
30+
sub-sequence.
31+
*/
32+
public class LongestWordInDictonary {
33+
34+
/**
35+
* Main method
36+
* @param args
37+
*/
38+
public static void main(String[] args) throws Exception{
39+
List<String> dict = Arrays.asList("ale","apple","monkey","plea");
40+
System.out.println(new LongestWordInDictonary().findLongestWord("abpcplea", dict));
41+
}
42+
43+
public String findLongestWord(String s, List<String> d) {
44+
Collections.sort(d, Comparator.comparing(String::length).reversed().thenComparing(String::compareTo));
45+
for(String str : d){
46+
if(str.length() <= s.length()){
47+
int i = 0, j = 0;
48+
for(int l1 = s.length(), l2 = str.length(); i < l1 && j < l2; ){
49+
if(s.charAt(i) == str.charAt(j)){
50+
i++; j++;
51+
} else{
52+
i++;
53+
}
54+
}
55+
if(j >= str.length()) return str;
56+
}
57+
}
58+
return "";
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 15/02/2018.
5+
* Given a binary search tree with non-negative values, find the minimum absolute difference between values of any
6+
* two nodes.
7+
8+
Example:
9+
10+
Input:
11+
12+
1
13+
\
14+
3
15+
/
16+
2
17+
18+
Output:
19+
1
20+
21+
Explanation:
22+
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
23+
Note: There are at least two nodes in this BST.
24+
*/
25+
public class MinimumAbsoluteDifferenceInBST {
26+
27+
public static class TreeNode {
28+
int val;
29+
TreeNode left;
30+
TreeNode right;
31+
TreeNode(int x) { val = x; }
32+
}
33+
34+
int min = Integer.MAX_VALUE;
35+
36+
public static void main(String[] args) throws Exception{
37+
TreeNode root = new TreeNode(1);
38+
root.right = new TreeNode(2);
39+
root.right.right = new TreeNode(3);
40+
new MinimumAbsoluteDifferenceInBST().getMinimumDifference(root);
41+
}
42+
43+
public int getMinimumDifference(TreeNode root) {
44+
getMin(root, null);
45+
return min;
46+
}
47+
48+
private Integer getMin(TreeNode node, Integer prev){
49+
if(node == null) return prev;
50+
Integer left = getMin(node.left, prev);
51+
if(left != null){
52+
min = Math.min(min, Math.abs(node.val - left));
53+
}
54+
return getMin(node.right, node.val);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)