Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions PalindromPartitioning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class Solution {
public IList<IList<string>> Partition(string s) {
List<IList<string>> result = new ();
helper(s,0,new List<string>(),result);
return result;
}

public void helper(string s,int pivot,List<string> path, List<IList<string>> result)
{
if(s.Length==pivot)
{
result.Add(new List<string>(path));
return;
}
for(int i=pivot;i<s.Length;i++)
{
string curstr = s.Substring(pivot,i-pivot + 1);
if(IsPalindrome(curstr))
{
path.Add(curstr);
helper(s,i+1,path,result);
path.RemoveAt(path.Count - 1);
}
}
}
public bool IsPalindrome(string s)
{
int i = 0 ;int j = s.Length - 1;
while(i <= j)
{
if(s[i++]!=s[j--])
{
return false;
}
}
return true;
}
}
// Time Complexity:
// O(2^n * n) -> For n length string, exponential partitions × (palindrome checks + creating substrings)

// Space Complexity:
// O(n^2) (substring creation and recursion stack)
21 changes: 21 additions & 0 deletions Subsets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Solution
{
public IList<IList<int>> Subsets(int[] nums)
{
List<IList<int>> result = new();
result.Add(new List<int>());
for (int i = 0; i < nums.Length; i++)
{
int size = result.Count;
for (int j = 0; j < size; j++)
{
List<int> subset = new List<int>(result[j]);
subset.Add(nums[i]);
result.Add(subset);
}
}

return result;
}
}
//Time and space - O(n * 2^n)