diff --git a/company/amazon/BestTimeToBuyAndSellStock.java b/company/amazon/BestTimeToBuyAndSellStock.java index 5e013d71..b5a5b647 100644 --- a/company/amazon/BestTimeToBuyAndSellStock.java +++ b/company/amazon/BestTimeToBuyAndSellStock.java @@ -12,6 +12,12 @@ // Output: 0 // In this case, no transaction is done, i.e. max profit = 0. +// new comments +// new comments +// new comments +// new comments +// new comments +// new comments public class BestTimeToBuyAndSellStock { public int maxProfit(int[] prices) { diff --git a/company/amazon/EncodeAndDecodeTinyURL.java b/company/amazon/EncodeAndDecodeTinyURL.java index 51158ced..39d61988 100644 --- a/company/amazon/EncodeAndDecodeTinyURL.java +++ b/company/amazon/EncodeAndDecodeTinyURL.java @@ -5,34 +5,44 @@ //encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL //and the tiny URL can be decoded to the original URL. -public class EncodeAndDecodeTinyURL { - HashMap map = new HashMap(); - String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - int count = 1; - - public String getKey() { - String key = ""; - while(count > 0) { - count--; - key += characters.charAt(count); - count /= characters.length(); +public class WordSearch { + public boolean exist(char[][] board, String word) { + char[] w = word.toCharArray(); + + for(int i = 0; i < board.length; i++) { + for(int j = 0; j < board[0].length; j++) { + if(search(board, i, j, w, 0)) { + return true; + } + } } - return key; + return false; } - // Encodes a URL to a shortened URL. - public String encode(String longUrl) { - String key = getKey(); - map.put(key, longUrl); - count++; - - return "http://tinyurl.com/" + key; - } + + public boolean search(char[][] board, int i, int j, char[] w, int index) { + if(index == w.length) { + return true; + } + + if(i < 0 || i >= board.length || j < 0 || j >= board[0].length) { + return false; + } + + if(board[i][j] != w[index]) { + return false; + } + + board[i][j] ^= 256; + + boolean exist = search(board, i + 1, j, w, index + 1) || + search(board, i - 1, j, w, index + 1) || + search(board, i, j + 1, w, index + 1) || + search(board, i, j - 1, w, index + 1); + board[i][j] ^= 256; - // Decodes a shortened URL to its original URL. - public String decode(String shortUrl) { - return map.get(shortUrl.replace("http://tinyurl.com/", "")); + return exist; } }