Skip to content

Commit

Permalink
Merge pull request cpsumsu#1 from awepo-pro/main
Browse files Browse the repository at this point in the history
update leetcode 17 and gitignore file
  • Loading branch information
tom-choi committed Nov 18, 2023
2 parents 4861ccd + 749752e commit 92a43ae
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# OS
.DS_Store
33 changes: 33 additions & 0 deletions leetcode/17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

class Solution {
public:
// use hash map store as well
const vector<string> pad = {
"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};

vector<string> letterCombinations(string digits) {
if (digits.empty()) return {};

vector<string> result;
// very important to append empty string, otherwise line 19 would not implement for empty vector
result.push_back("");

for (auto digit : digits) {
vector<string> tmp;

for (auto candidate : pad[digit - '2']) {
// for each element in result, append the new character
for (auto s : result) {
// store them into tmp so that tmp currently becomes the answer
tmp.push_back(s + candidate);
}
}

result.swap(tmp);
}

return result;
}
};

0 comments on commit 92a43ae

Please sign in to comment.