This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private int index = 0; | |
public String decodeString(String s) { | |
if (s == null || s.length() == 0) { | |
return ""; | |
} | |
int current_count = 0; | |
StringBuilder sb = new StringBuilder(); | |
while (index < s.length()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean exist(char[][] board, String word) { | |
for (int i = 0; i < board.length; i++) { | |
for (int j = 0; j < board[i].length; j++) { | |
if (dfs(board, word, 0, i, j)) { | |
return true; | |
} | |
} | |
} | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int numIslands(char[][] grid) { | |
int result = 0; | |
boolean[][] visited = new boolean[grid.length][grid[0].length]; | |
Queue<int[]> queue = new LinkedList<>(); | |
for (int row = 0; row < grid.length; row++) { | |
for (int col = 0; col < grid[0].length; col++) { | |
if (grid[row][col] == '1' && !visited[row][col]) { | |
visited[row][col] = true; | |
queue.offer(new int[]{row, col}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class EncodeAndDecode { | |
// Encodes a list of strings to a single string. | |
public String encode(List<String> strs) { | |
StringBuilder ans = new StringBuilder(); | |
for (String s : strs) { | |
ans.append((char) s.length()).append(s); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int countSubstrings(String s) { | |
if (s == null || s.length() == 0) { | |
return 0; | |
} | |
int result = 0; | |
for (int i = 0; i < s.length(); i++) { | |
for (int j = i; j < s.length(); j++) { | |
result += isPalindrome(s, i, j) ? 1 : 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public String longestPalindrome(String s) { | |
int window = s.length(); | |
while (window > 0) { | |
for (int left = 0; left <= s.length() - window; left++) { | |
if (isPalindrome(s, left, left + window - 1)) { | |
return s.substring(left, left + window); | |
} | |
} | |
window--; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isPalindrome(String s) { | |
for (int left = 0, right = s.length() - 1; left < right; left++, right--) { | |
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { | |
left++; | |
} | |
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { | |
right--; | |
} | |
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String str = s.chars() | |
.mapToObj(i -> (char) i) | |
.filter(Character::isLetter) | |
.map(String::valueOf) | |
.collect(Collectors.joining()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isValid(String s) { | |
if (s == null || s.length() == 0) { | |
return false; | |
} | |
Map<Character, Character> map = new HashMap<>(); | |
map.put(')', '('); | |
map.put('}', '{'); | |
map.put(']', '['); | |
Stack<Character> stack = new Stack<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<String>> groupAnagrams(String[] strs) { | |
List<List<String>> result = new ArrayList<>(); | |
Map<String, List<String>> map = new HashMap<>(); | |
for (String str : strs) { | |
String key = Stream.of(str.split("")) | |
.sorted() | |
.collect(Collectors.joining()); | |
map.computeIfAbsent(key, v -> new ArrayList<>()).add(str); | |
} |
NewerOlder