-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
49 lines (46 loc) · 1.19 KB
/
Copy pathsolution.java
File metadata and controls
49 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 131. Palindrome Partitioning
// https://leetcode.com/problems/palindrome-partitioning/
// Medium | Java | Accepted 2025-12-19
// Runtime 8 ms | Memory 65.4 MB
class Solution {
List<List<String>> ans = new ArrayList<>();
String glob;
public List<List<String>> partition(String s) {
glob = s;
recurse(new ArrayList<>(), 0);
return ans;
}
public void recurse(List<String> temp, int index)
{
if(index==glob.length())
{
ans.add(new ArrayList<>(temp));
return;
}
for(int i = index; i<glob.length(); i++)
{
String s = glob.substring(index, i+1);
if(palindrome(s))
{
temp.add(s);
recurse(temp, i+1);
temp.remove(temp.size()-1);
}
}
}
public boolean palindrome(String check)
{
int pointer1 = 0;
int pointer2 = check.length()-1;
while(pointer1<=pointer2)
{
if(check.charAt(pointer1)!=check.charAt(pointer2))
{
return false;
}
pointer1++;
pointer2--;
}
return true;
}
}