Longest Palindromic Substring

class Solution {
public:
    string longestPalindrome(string s) {
        if (s.empty()) return "";
        if (s.size() == 1) return s;
        int min_start = 0, max_len = 1;
        for (int i = 0; i < s.size();) {
            if (s.size() - i <= max_len / 2) break;
            int j = i, k = i;
            while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
            i = k+1;
            while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
            int new_len = k - j + 1;
            if (new_len > max_len) { min_start = j; max_len = new_len; }
        }
        return s.substr(min_start, max_len);
    }
};
2017/12/12 posted in  leetcode

Palindrome Pairs

class Solution {
public:
    bool isPalindrome(string s) {
        if (s.size() <= 1) return true;
        int i = 0;
        int j = s.size() - 1;
        while (i < j) {
            if (s[i++] != s[j--]) return false;
        }

        return true;
    }

    vector<vector<int>> palindromePairs(vector<string>& words) {
        vector<vector<int>> res;
        if (!words.size()) return res;
        unordered_map<string, int> word_idx;
        for (int i = 0; i < words.size(); ++i) {
            word_idx[words[i]] = i;
        }
        vector<int> slu(2);
        for (int i = 0; i < words.size(); ++i) {
            int len = words[i].length();
            for (int l = 0; l <= len; ++l) {
                string left = words[i].substr(0, l);
                string right = words[i].substr(l);
                string rleft = left;
                string rright = right;
                reverse(rleft.begin(), rleft.end());
                reverse(rright.begin(), rright.end());
                if (word_idx.find(rleft) != word_idx.end()) {
                    if (word_idx[rleft] != i && isPalindrome(right)) {
                        res.push_back({i,word_idx[rleft]});
                    }

                }
                if (l != 0 && word_idx.find(rright) != word_idx.end()) {
                    if (word_idx[rright] != i && isPalindrome(left)) {
                        res.push_back({word_idx[rright],i});
                    }

                }

            }
        }
        return res;
    }
};
2017/12/12 posted in  leetcode

Intersection of Two Arrays II

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> map;
        vector<int> res;
        for(auto num1:nums1){
            map[num1]++;
        }

        for(auto num2:nums2){
            if (map[num2]>0) {
                map[num2]--;
                res.push_back(num2);
            }
        }
        return res;
    }
};
2017/12/12 posted in  leetcode

Sort Characters By Frequency

class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char, int> map;
        for (int i =0; i<s.length(); i++) {
            map[s[i]]++;
        }

        sort(s.begin(), s.end(), [&map](char a ,char b){
            return map[a]>map[b] || (map[a] == map[b] && a > b);
        });
        
        return s;
    }
};
2017/12/11 posted in  leetcode

First Unique Character in a String

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char, int> map;
        for (int i =0; i<s.length(); i++) {
            map[s[i]]++;
        }
        for (int i =0; i<s.length(); i++) {
            if (map[s[i]]==1) {
                return i;
            }
        }
        return -1;
    }
};
2017/12/11 posted in  leetcode

Minimum Moves to Equal Array Elements II

class Solution {
public:
    int minMoves2(vector<int>& nums) {
        int n = nums.size();
        if(n <= 1) return 0;
        sort(nums.begin(), nums.end());
        int mid = nums.size()/2 ,res = 0;
        for(int i = 0; i < mid; ++i)
        {
            res+=nums[mid]-nums[i];
        }
        for (int j = n-1; j>mid; j--) {
            res+=nums[j]-nums[mid];
        }
        return res;
    }
};
2017/12/11 posted in  leetcode