784. Letter Case Permutation

class Solution { public: vector<string> permutationHelper(int curr, int maxSize, string S, string & chosen, vector<string> & res){ // base case if (curr == maxSize){\ res.push_back(chosen); return res; } if (std::isalpha(S[curr])){ // choose chosen += std::tolower(S[curr]); // explore permutationHelper(curr+1, maxSize, S, chosen, res); // unchoose chosen.pop_back(); // choose again chosen += std::toupper(S[curr]); // explore again permutationHelper(curr+1, maxSize, S, chosen, res); chosen.pop_back(); } else{ chosen += S[curr]; permutationHelper(curr+1, maxSize, S, chosen, res); chosen.pop_back(); } } vector<string> letterCasePermutation(string S) { vector<string> res; string chosen = ""; return permutationHelper(0, S.size()-1, S, chosen, res); } };
Wrong solution. Error message:
Line 3: execution reached the end of a value-returning function without returning a value
Test case: "a1b2"
20180918

1 Response

1. There is a segmentation fault... why?
2. Another bug is using cur == maxSize will result in not reaching the end!!! why???

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.