File tree 2 files changed +82
-0
lines changed
2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <String > findRepeatedDnaSequences (String s ) {
3
+ Map <String , Integer > map = new HashMap <>();
4
+ Set <String > result = new HashSet <>();
5
+
6
+ if (s .length () < 10 ) return new ArrayList <String >(result );
7
+
8
+ for (int i =0 ; i <=s .length ()-10 ; i ++){
9
+ String substr = s .substring (i , i +10 );
10
+ map .put (substr , map .getOrDefault (substr , 0 )+1 );
11
+ if (map .get (substr )>1 )
12
+ result .add (substr );
13
+ }
14
+
15
+ return new ArrayList <String >(result );
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ /*
12
+ Recursion: Do a DFS recursively
13
+ Time & Space: O(n)
14
+
15
+ */
16
+
17
+ public boolean isUnivalTree (TreeNode root ) {
18
+ if (root == null )
19
+ return true ;
20
+
21
+ return dfs (root , root .val );
22
+
23
+ }
24
+
25
+ public boolean dfs (TreeNode node , int val ){
26
+ if (node == null )
27
+ return true ;
28
+ if (node .val != val )
29
+ return false ;
30
+ return dfs (node .left , val ) && dfs (node .right , val );
31
+ }
32
+
33
+
34
+ /*
35
+
36
+ Approach 2: Iteratively
37
+ Time and Space: O(n)
38
+ */
39
+
40
+ public boolean isUnivalTree (TreeNode root ) {
41
+ if (root == null )
42
+ return true ;
43
+
44
+ Queue <TreeNode > queue = new LinkedList <>();
45
+ queue .add (root );
46
+
47
+ while (!queue .isEmpty ()){
48
+ int size = queue .size ();
49
+ TreeNode node = queue .remove ();
50
+
51
+ if (root .val != node .val )
52
+ return false ;
53
+
54
+ if (node .left != null ) queue .add (node .left );
55
+ if (node .right != null ) queue .add (node .right );
56
+
57
+ }
58
+
59
+ return true ;
60
+ }
61
+ }
62
+
63
+
64
+
65
+
You can’t perform that action at this time.
0 commit comments