-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from seungyonkim/main
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
YonaKim/Week9/2023-11-12-1593-Split-a-String-Into-the-Max-Number-of-Unique-Substrings.java
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
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(); | ||
} | ||
} |
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
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); | ||
} | ||
} |
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
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; | ||
} | ||
} |