Skip to content

Commit 50741a4

Browse files
committed
leetcode
1 parent fd53b27 commit 50741a4

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
3+
4+
5+
-* 131. Palindrome Partitioning *-
6+
7+
Given a string s, partition s such that every
8+
substring
9+
of the partition is a
10+
palindrome
11+
. Return all possible palindrome partitioning of s.
12+
13+
14+
15+
Example 1:
16+
17+
Input: s = "aab"
18+
Output: [["a","a","b"],["aa","b"]]
19+
Example 2:
20+
21+
Input: s = "a"
22+
Output: [["a"]]
23+
24+
25+
Constraints:
26+
27+
1 <= s.length <= 16
28+
s contains only lowercase English letters.
29+
30+
31+
*/
32+
class A {
33+
bool checkPalindrome(String str, int startIndex, int lastIndex) {
34+
while (startIndex <= lastIndex) {
35+
if (str.codeUnitAt(startIndex) != str.codeUnitAt(lastIndex)) return false;
36+
startIndex++;
37+
lastIndex--;
38+
}
39+
return true;
40+
}
41+
42+
void palindromePartition(
43+
int index, List<String> ds, List<List<String>> output, String str) {
44+
if (index == str.length) {
45+
// output.add([...ds]..toList());
46+
output.add([...ds]);
47+
return;
48+
}
49+
for (int i = index; i < str.length; i++) {
50+
if (checkPalindrome(str, index, i)) {
51+
ds.add(str.substring(index, i + 1));
52+
palindromePartition(i + 1, ds, output, str);
53+
ds.remove(ds.length - 1);
54+
}
55+
}
56+
}
57+
58+
List<List<String>> partition(String s) {
59+
List<List<String>> output = <String>[].map((e) => <String>[]).toList();
60+
List<String> ds = [];
61+
palindromePartition(0, ds, output, s);
62+
return output;
63+
}
64+
}
65+
66+
class B {
67+
bool isPalindrome(String s) {
68+
int left = 0, right = s.length - 1;
69+
while (left <= right) {
70+
if (s[left] != s[right]) return false;
71+
left++;
72+
right--;
73+
}
74+
return true;
75+
}
76+
77+
void helper(String s, List<String> step, List<List<String>> result) {
78+
// Base case
79+
if (s.length == 0) {
80+
result.add(step.toList());
81+
return;
82+
}
83+
for (int i = 1; i <= s.length; i++) {
84+
String temp = s.substring(0, i);
85+
if (!isPalindrome(temp))
86+
continue; // only do backtracking when current string is palindrome
87+
88+
step.add(temp); // choose
89+
helper(s.substring(i, s.length), step, result); // explore
90+
step.remove(step.length - 1); // un-choose
91+
}
92+
return;
93+
}
94+
95+
List<List<String>> partition(String s) {
96+
// Backtracking
97+
// Edge case
98+
if (s.length == 0) return [];
99+
100+
List<List<String>> result = [];
101+
helper(s, [], result);
102+
return result;
103+
}
104+
}
105+
106+
class C {
107+
bool checkPalindrome(String str, int startIndex, int lastIndex) {
108+
while (startIndex <= lastIndex) {
109+
if (str[startIndex] != str[lastIndex]) return false;
110+
startIndex++;
111+
lastIndex--;
112+
}
113+
return true;
114+
}
115+
116+
void palindromePartition(
117+
int index, List<String> ds, List<List<String>> output, String str) {
118+
if (index == str.length) {
119+
output.add(List.filled(ds.length - 1, ""));
120+
return;
121+
}
122+
for (int i = index; i < str.length; i++) {
123+
if (checkPalindrome(str, index, i)) {
124+
ds.add(str.substring(index, i - index + 1));
125+
palindromePartition(i + 1, ds, output, str);
126+
ds.removeLast();
127+
}
128+
}
129+
}
130+
131+
List<List<String>> partition(String s) {
132+
List<List<String>> output = [];
133+
List<String> ds = [];
134+
palindromePartition(0, ds, output, s);
135+
return output;
136+
}
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
func partition(s string) [][]string {
4+
result := [][]string{}
5+
seq := make([]string, 0, len(s))
6+
dfs(s, &result, seq)
7+
return result
8+
}
9+
10+
func dfs(s string, result *[][]string, seq []string) {
11+
if len(s) == 0 {
12+
cp := make([]string, len(seq))
13+
copy(cp, seq)
14+
*result = append(*result, cp)
15+
return
16+
}
17+
18+
for i := 1; i <= len(s); i++ {
19+
subStr := s[:i]
20+
if isPalindrome(subStr) {
21+
dfs(s[i:], result, append(seq, subStr))
22+
}
23+
}
24+
}
25+
26+
func isPalindrome(s string) bool {
27+
l, r := 0, len(s)-1
28+
for l < r {
29+
if s[l] != s[r] {
30+
return false
31+
}
32+
l++
33+
r--
34+
}
35+
return true
36+
}

PalindromePartitioning/palindrome_partitioning.md

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
191191
- [**974.** SubArray Sums Divisible by K](SubarraySumsDivisibleByK/subarray_sums_divisible_by_k.dart)
192192
- [**491.** Non-decreasing Subsequences](Non-DecreasingSubsequences/non_decreasing_subsequences.dart)
193193
- [**93.** Restore IP Addresses](RestoreIPAddresses/restore_ip_addresses.dart)
194+
- [**131.** Palindrome Partitioning](PalindromePartitioning/palindrome_partitioning.dart)
194195

195196
## Reach me via
196197

0 commit comments

Comments
 (0)