File tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maxProfit (int [] prices ) {
3+ int minPrice = prices [0 ];
4+ int maxProfit = 0 ;
5+
6+ for (int p : prices ){
7+ if (minPrice < p && maxProfit < p - minPrice ){
8+ maxProfit = p - minPrice ;
9+ }
10+ if (minPrice > p ){
11+ minPrice = p ;
12+ }
13+ }
14+ return maxProfit ;
15+ }
16+ }
17+
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ /*
3+ * @param strs: a list of strings
4+ * @return: encodes a list of strings to a single string.
5+ */
6+ public String encode (List <String > strs ) {
7+ StringBuilder sb = new StringBuilder ();
8+ for (String s : strs ) {
9+ sb .append (s .length ()).append ('#' ).append (s );
10+ }
11+ return sb .toString ();
12+ }
13+
14+ /*
15+ * @param str: A single encoded string
16+ * @return: decodes the single string to a list of strings
17+ */
18+ public List <String > decode (String str ) {
19+ List <String > result = new ArrayList <>();
20+ int i = 0 ;
21+ while (i < str .length ()) {
22+ int j = i ;
23+ while (str .charAt (j ) != '#' ) {
24+ j ++;
25+ }
26+ int length = Integer .parseInt (str .substring (i , j ));
27+ j ++;
28+ result .add (str .substring (j , j + length ));
29+ i = j + length ;
30+ }
31+ return result ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public List <List <String >> groupAnagrams (String [] strs ) {
3+ Map <String , List <String >> groups = new HashMap <>();
4+
5+ for (String word : strs ){
6+ int [] characters = new int [26 ];
7+ for (char c : word .toCharArray ()){
8+ characters [c - 'a' ]++;
9+ }
10+
11+ String countCode = Arrays .toString (characters );
12+ if (!groups .containsKey (countCode )){
13+ groups .put (countCode , new ArrayList <String >());
14+ }
15+ List <String > temp = groups .get (countCode );
16+ temp .add (word );
17+ }
18+
19+ List <List <String >> answer = new ArrayList <>();
20+ for (List <String > g : groups .values ()){
21+ answer .add (g );
22+ }
23+
24+ return answer ;
25+ }
26+ }
27+
Original file line number Diff line number Diff line change 1+ class Trie {
2+ Set <String > dict ;
3+
4+ public Trie () {
5+ dict = new TreeSet <>();
6+ }
7+
8+ public void insert (String word ) {
9+ dict .add (word );
10+ }
11+
12+ public boolean search (String word ) {
13+ if (dict .contains (word )) return true ;
14+ return false ;
15+ }
16+
17+ public boolean startsWith (String prefix ) {
18+ for (String saved : dict ){
19+ int count = 0 ;
20+ if (prefix .compareTo (saved ) > 0 ) continue ;
21+ if (prefix .length () > saved .length ()) continue ;
22+
23+ for (int i = 0 ; i < prefix .length (); i ++){
24+ if (prefix .charAt (i ) != saved .charAt (i )) break ;
25+ count ++;
26+ }
27+ if (count == prefix .length ()) return true ;
28+ }
29+ return false ;
30+ }
31+ }
32+
33+ /**
34+ * Your Trie object will be instantiated and called as such:
35+ * Trie obj = new Trie();
36+ * obj.insert(word);
37+ * boolean param_2 = obj.search(word);
38+ * boolean param_3 = obj.startsWith(prefix);
39+ */
40+
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public boolean wordBreak (String s , List <String > wordDict ) {
3+ boolean [] dp = new boolean [s .length () + 1 ];
4+ dp [0 ] = true ;
5+
6+ for (int n = 1 ; n <= s .length (); n ++) {
7+ for (String word : wordDict ) {
8+ if (n >= word .length () && s .substring (n - word .length (), n ).equals (word )) {
9+ dp [n ] = dp [n - word .length ()];
10+ }
11+ if (dp [n ]) {
12+ break ;
13+ }
14+ }
15+ }
16+
17+ return dp [s .length ()];
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments