Skip to content

Instantly share code, notes, and snippets.

View TheGreatJoules's full-sized avatar
Working from home

Julian Carachure TheGreatJoules

Working from home
  • 15:01 (UTC -07:00)
View GitHub Profile
@TheGreatJoules
TheGreatJoules / Leetcode_394_Solution_A.java
Created October 23, 2023 17:55
[Leetcode/394] Decode String
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()) {
@TheGreatJoules
TheGreatJoules / Leetcode_79_Solution_A.java
Created October 23, 2023 17:36
[Leetcode/79] Word Search #dfs #backtracking
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;
@TheGreatJoules
TheGreatJoules / Leetcode_200_Solution_A.java
Last active October 23, 2023 16:47
[Leetcode/200] Number Islands #bfs #dfs
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});
@TheGreatJoules
TheGreatJoules / Leetcode_647_Solution_A.java
Created October 23, 2023 07:26
[Leetcode/647] Encode and Decode Strings #blind75
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);
@TheGreatJoules
TheGreatJoules / Leetcode_647_Solution_A.java
Last active October 23, 2023 06:18
[Leetcode/647] Palindromic Substrings #permutation #blind75
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;
}
@TheGreatJoules
TheGreatJoules / Leetcode_5_Solution_A.java
Last active October 22, 2023 21:41
[Leetcode/5] Longest Palindromic Substring #bruteforce #blind75
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--;
@TheGreatJoules
TheGreatJoules / Leetcode_125_Solution_A.java
Last active October 22, 2023 08:05
[Leetcode/125] Valid Palindrome #slidingwindow #blind75
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))) {
@TheGreatJoules
TheGreatJoules / StringStreamFilter.java
Created October 21, 2023 21:16
[Java/StringStreamFilter] Simple string filter
String str = s.chars()
.mapToObj(i -> (char) i)
.filter(Character::isLetter)
.map(String::valueOf)
.collect(Collectors.joining());
@TheGreatJoules
TheGreatJoules / Leetcode_20_Solution_A.java
Last active October 22, 2023 08:04
[Leetcode/20] Valid Parentheses #stack #blind75
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<>();
@TheGreatJoules
TheGreatJoules / Leetcode_49_Solution_A.java
Last active October 22, 2023 08:04
[Leetcode/49] Group Anagrams #sort #map #blind75
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);
}