algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

commit 69e20a4474ef66c9db5778893a9c162ef40e98c6
parent eb3d1bbf66fea2c0503eff05415f677f6d040a95
Author: Andrew Laack <andrew@laack.co>
Date:   Wed,  9 Sep 2026 22:13:15 -0500

cpp and python permutation problem

Diffstat:
Apermutations-ii/permutations-ii.cpp | 39+++++++++++++++++++++++++++++++++++++++
Apermutations-ii/permutations-ii.py | 34++++++++++++++++++++++++++++++++++
Mplan.txt | 3++-
3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/permutations-ii/permutations-ii.cpp b/permutations-ii/permutations-ii.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + vector<vector<int>> genPerms(unordered_map<int,int> options, vector<int> current) { + if(options.size() == 0) { + return vector<vector<int>> {current}; + } + vector<vector<int>> result {}; + auto currentOption = options; + for(auto option: currentOption) { + bool erased = false; + options[option.first] -= 1; + current.push_back(option.first); + if (options[option.first] <= 0) { + options.erase(option.first); + erased = true; + } + auto next = genPerms(options, current); + for(auto ls: next) { + result.push_back(ls); + } + current.pop_back(); + options[option.first] += 1; + } + + return result; + + } + + vector<vector<int>> permuteUnique(vector<int>& nums) { + unordered_map<int,int> fullMap {}; + + for(auto num : nums) { + fullMap[num] += 1; + } + + return genPerms(fullMap, vector<int>{}); + } +}; diff --git a/permutations-ii/permutations-ii.py b/permutations-ii/permutations-ii.py @@ -0,0 +1,34 @@ +class Solution: + + def permute(self,remaining, current): + + if len(remaining) == 0: + self.result.append(current.copy()) + return + + k = list(remaining.keys()) + + for rem in k: + count = remaining[rem] + count -= 1 + if count == 0: + del remaining[rem] + else: + remaining[rem] = count + current.append(rem) + self.permute(remaining, current) + remaining[rem] = count + 1 + current.pop() + + return + + def permuteUnique(self, nums: List[int]) -> List[List[int]]: + self.result = [] + numDict = {} + for num in nums: + if numDict.get(num) is None: + numDict[num] = 1 + else: + numDict[num] += 1 + self.permute(numDict, []) + return self.result diff --git a/plan.txt b/plan.txt @@ -1,7 +1,8 @@ plan ==== these are the things I want to learn, ordered - x number of palindrome substrings + x number of palindrome substrings (similar to interview where print out all palindrome's len >= 3) x prim's algo (visualized in python) + x permutation problem (similar to interview where print all permutations of a string where only certain chars may be moved) - kahn's algo (visualized in python) - union find