Skip to content

Commit a22fc66

Browse files
author
C5141506
committed
leetcode problem
1 parent 5963278 commit a22fc66

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package java_problem.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
7+
8+
public class Palindrom {
9+
// Online Java Compiler
10+
// Use this editor to write, compile and run your Java code online
11+
public static void main(String[] args) {
12+
HashSet<List<String>> res = new HashSet<List<String>>();
13+
SolutionPalindrome solutionPalindrome=new SolutionPalindrome();
14+
String s = "cdd";
15+
solutionPalindrome.partition(s, res);
16+
System.out.println(res);
17+
}
18+
19+
20+
}
21+
class SolutionPalindrome {
22+
23+
public List<List<String>> partition(String s,HashSet<List<String>> res) {
24+
int n = s.length();
25+
if(n==0) return new ArrayList<List<String>>();
26+
partition(s, 0, new ArrayList<String>(),res);
27+
return new ArrayList(res);
28+
}
29+
30+
public void partition(String s, int start, List<String> ls,HashSet<List<String>> res) {
31+
32+
if (start == s.length()) {
33+
res.add(new ArrayList<String>(ls));
34+
};
35+
36+
for (int end = start; end < s.length(); end++) {
37+
if(isPalindrome(s, start, end)) {
38+
ls.add(s.substring(start, end+1));
39+
partition(s, end+1, ls,res);
40+
ls.remove(ls.size()-1);
41+
}
42+
}
43+
44+
}
45+
46+
public boolean isPalindrome(String s, int start, int end) {
47+
while (start < end) {
48+
if (s.charAt(start) != s.charAt(end)) {
49+
return false;
50+
}
51+
start++;
52+
end--;
53+
}
54+
return true;
55+
}
56+
}
57+
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package java_problem.graph;
2+
3+
public class SurroundedRegion {
4+
public static void main(String[] args) {
5+
char[][] board =
6+
{{'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'}};
7+
Solution solution = new Solution();
8+
solution.solve(board);
9+
}
10+
}
11+
12+
class Solution {
13+
public void solve(char[][] board) {
14+
int rowNo = board.length;
15+
int colNo = board[0].length;
16+
17+
visitBoundary(board, rowNo, colNo);
18+
flip(board, rowNo, colNo);
19+
20+
}
21+
22+
public void flip(char[][] board, int row, int col) {
23+
for (int i = 0; i < row; i++) {
24+
for (int j = 0; j < col; j++) {
25+
if (board[i][j] == 'O')
26+
board[i][j] = 'X';
27+
28+
}
29+
}
30+
}
31+
32+
public void visitBoundary(char[][] board, int rowNo, int colNo) {
33+
//visit top row to check O
34+
for (int col = 0; col < colNo; col++) {
35+
if (board[0][col] == 'O') {
36+
board[0][col] = 'V';
37+
dfs(0, col, board);
38+
}
39+
}
40+
41+
//visit bottom row to check O
42+
for (int col = 0; col < colNo; col++) {
43+
if (board[rowNo - 1][col] == 'O') {
44+
board[rowNo - 1][col] = 'V';
45+
dfs(rowNo, col, board);
46+
}
47+
}
48+
49+
//visit left column to check O
50+
for (int row = 0; row < rowNo; row++) {
51+
if (board[row][0] == 'O') {
52+
board[row][0] = 'V';
53+
dfs(row, 0, board);
54+
}
55+
}
56+
57+
//visit right column to check O
58+
for (int row = 0; row < rowNo; row++) {
59+
if (board[colNo - 1][row] == 'O') {
60+
board[colNo - 1][row] = 'V';
61+
dfs(colNo - 1, row, board);
62+
}
63+
}
64+
}
65+
66+
public void dfs(int row, int col, char[][] grid) {
67+
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] == 'V') {
68+
return;
69+
}
70+
71+
//mark as visited cell
72+
if (grid[row][col] == 'O')
73+
grid[row][col] = 'V';
74+
75+
dfs(row - 1, col, grid);
76+
dfs(row, col - 1, grid);
77+
dfs(row, col + 1, grid);
78+
dfs(row + 1, col, grid);
79+
}
80+
81+
}
82+
83+
84+
85+
86+

0 commit comments

Comments
 (0)