Skip to content

Commit

Permalink
Merge pull request #885 from seungyonkim/main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Nov 13, 2023
2 parents db89020 + 3ad3d0b commit ced432a
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.ArrayList;

class Solution {
public int maxUniqueSplit(String s) {
String subString = "";

ArrayList<String> subStrings = new ArrayList<String>();

while(s.length() > 0) {
subString += s.charAt(0);
s = s.substring(1);

if(!subStrings.contains(subString)) {
subStrings.add(subString);
subString = "";
}

//if we reach end of string and there is duplicate
}

return subStrings.size();
}
}
43 changes: 43 additions & 0 deletions YonaKim/Week9/2023-11-12-489-Robot-Room-Cleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
// Directions for going clockwise: 0: 'up', 1: 'right', 2: 'down', 3: 'left'
int[][] directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
Set<Pair<Integer, Integer>> visited = new HashSet<>();
Robot robot;

// Move the robot backward
public void goBack() {
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnRight();
robot.turnRight();
}

// Backtrack and explore the room
public void backtrack(int row, int col, int d) {
visited.add(new Pair<>(row, col));
robot.clean();

// Explore in all four directions
for (int i = 0; i < 4; ++i) {
int newD = (d + i) % 4;
int newRow = row + directions[newD][0];
int newCol = col + directions[newD][1];

// Check if the new cell is not visited and the robot can move to that cell
if (!visited.contains(new Pair<>(newRow, newCol)) && robot.move()) {
backtrack(newRow, newCol, newD);
goBack();
}

// Turn the robot following the chosen direction: clockwise
robot.turnRight();
}
}

// Clean the entire room using backtracking
public void cleanRoom(Robot robot) {
this.robot = robot;
backtrack(0, 0, 0);
}
}
51 changes: 51 additions & 0 deletions YonaKim/Week9/2023-11-12-79-Word-Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
private char[][] board;
private int numRows;
private int numCols;

public boolean searchWord(char[][] gameBoard, String targetWord) {
this.board = gameBoard;
this.numRows = gameBoard.length;
this.numCols = gameBoard[0].length;

for (int row = 0; row < numRows; ++row)
for (int col = 0; col < numCols; ++col)
if (backtrackSearch(row, col, targetWord, 0))
return true;
return false;
}

protected boolean backtrackSearch(int row, int col, String word, int index) {
// Base case of recursion. If the word to be matched is empty, return true.
if (index >= word.length())
return true;

// Check if the current state is invalid:
// If the letter in the current cell does not match the first letter of the word,
// or if the position of the cell is out of the boundary of the board.
if (row < 0 || row == numRows || col < 0 || col == numCols
|| board[row][col] != word.charAt(index))
return false;

/* Step 3). Explore the neighbors in DFS */
// Explore neighbors in DFS (backtracking)
// Mark the current cell as visited (using any non-alphabetic letter)
// Then iterate through the up, down, right, left directions (order of direction does not matter)
boolean result = false;
// Mark the path before the next exploration
board[row][col] = '#';

int[] rowOffsets = {0, 1, 0, -1};
int[] colOffsets = {1, 0, -1, 0};
for (int direction = 0; direction < 4; ++direction) {
result = backtrackSearch(row + rowOffsets[direction], col + colOffsets[direction], word, index + 1);
if (result)
break;
}

// Revert the cell back to its original state
// Return the result of recursion
board[row][col] = word.charAt(index);
return result;
}
}

0 comments on commit ced432a

Please sign in to comment.