void helper(int i, int n, string &s, unordered_set<string> &st, string &cur) {
if(cur.size() == n) {
st.insert(cur);
return;
}
for(int j=i; j<n; j++) {
swap(s[i], s[j]);
cur.push_back(s[i]);
helper(i+1, n, s, st, cur);
cur.pop_back();
swap(s[i], s[j]);
}
}
vector<string> findPermutation(string &s) {
// Code here there
unordered_set<string> st;
string cur;
helper(0, s.size(), s, st, cur);
vector<string> res(st.begin(), st.end());
return res;
}
Tag: string
#796 Rotate String
Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s. A shift on s consists of moving the leftmost character of s to the rightmost position. For example, if s = “abcde”, then it will be “bcdea” after one shift. Example 1: Input: s = “abcde”, goal = “cdeab” Output: true Example 2: Input: s = “abcde”, goal = “abced” Output: false Solution…
#316 Remove Duplicate Letters
Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: s = “bcabc” Output: “abc” Example 2: Input: s = “cbacdcbc” Output: “acdb” Solution : string removeDuplicateLetters(string s) {map umap;map umap1;string result =…
#125 Valid Palindrome
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1: Input: s = “A man, a plan, a canal: Panama” Output: true Explanation: “amanaplanacanalpanama” is…
#344 Reverse String
Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Example 1: Input: s = [“h”,”e”,”l”,”l”,”o”] Output: [“o”,”l”,”l”,”e”,”h”] Example 2: Input: s = [“H”,”a”,”n”,”n”,”a”,”h”] Output: [“h”,”a”,”n”,”n”,”a”,”H”] Constraints: 1 <= s.length <= 105 s[i] is a printable ascii character. Solution :…