Skip to content

Instantly share code, notes, and snippets.

class Solution {
int count = 0;
int N = 0;
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
if (rooms == null || rooms.size() < 1) return true;
int n = rooms.size();
count = n;
N = n;
boolean[] visited = new boolean[n];
Set<Integer> canReach = new HashSet<>();
class Solution {
public List<Integer> splitIntoFibonacci(String S) {
List<Integer> res = new ArrayList<>();
int len = S.length();
for (int i = 1; i < len && i < 11; i++) {
if (S.charAt(0) == '0' && i > 1) return res;
for (int j = 1; j < len - i && j < 11; j++) {
if (S.charAt(i) == '0' && j > 1) continue;
long a = Long.valueOf(S.substring(0, i));
long b = Long.valueOf(S.substring(i, i + j));
class Solution {
public int numMagicSquaresInside(int[][] grid) {
if (grid == null || grid.length < 3 || grid[0].length < 3) return 0;
int m = grid.length, n = grid[0].length;
boolean[] visited = new boolean[9];
int[] rows = new int[3];
int[] cols = new int[3];
int[] diags = new int[2];
boolean flag = false;
/**
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
* interface Master {
* public int guess(String word) {}
* }
*/
class Solution {
private int match(String guess, String w) {
int match = 0;
class Solution {
public int[][] flipAndInvertImage(int[][] A) {
if (A == null || A.length < 1 || A[0].length < 1) return A;
int m = A.length, n = A[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = A[i][j];
A[i][j] = A[i][n - 1 - j] ^ 1;
A[i][n - 1 - j] = temp ^ 1;
class Solution {
public int countSubstrings(String s) {
if (s == null || s.length() < 0) return 0;
int count = 0, len = s.length();
boolean[][] isPalindrome = new boolean[len][len];
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j < len; j++) {
if (s.charAt(i) == s.charAt(j) && (j - i < 2 || isPalindrome[i + 1][j - 1])) {
isPalindrome[i][j] = true;
count++;
class Solution {
public List<List<Integer>> largeGroupPositions(String S) {
List<List<Integer>> res = new ArrayList<>();
int len = S.length();
if (len < 3) return res;
int start = 0, end = 1;
while (end < len) {
while (end < len && S.charAt(end) == S.charAt(end - 1)) {
end++;
}
class Solution {
public String maskPII(String S) {
StringBuilder res = new StringBuilder();
int idx = S.indexOf('@');
int len = S.length();
if (idx > 0) {
res.append(Character.toLowerCase(S.charAt(0)));
res.append("*****");
res.append(S.substring(idx - 1).toLowerCase());
} else {
class Solution {
public int uniqueLetterString(String S) {
char[] s = S.toCharArray();
int n = s.length;
int mod = 1000000007;
long ret = 0;
for(int i = 0;i < s.length;i++){
int l = i-1;
for(;l >= 0 && s[l] != s[i];l--);
int r = i+1;
public class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
List<String> res = new ArrayList<String>();
if (s == null || s.length() == 0) {
return res;
}
int max = 0;
for (String str : wordDict) {
max = Math.max(str.length(), max);
}