Similar Problems

Similar Problems not available

Swap For Longest Repeated Character Substring - Leetcode Solution

Companies:

LeetCode:  Swap For Longest Repeated Character Substring Leetcode Solution

Difficulty: Medium

Topics: string sliding-window hash-table  

Problem: Given a string s, you are allowed to swap two adjacent characters in the string once. You need to make s palindrome.

Return the minimum number of steps to do so.

A palindrome string is a string that reads the same backward as forward.

Example 1:

Input: s = "mamad" Output: 3 Explanation: You can swap the first 'a' with the second 'm' and swap the second 'a' with the third 'm'. The string becomes "madam" which is a palindrome.

Example 2:

Input: s = "asflkj" Output: -1 Explanation: Swap operation can not be performed.

Example 3:

Input: s = "aabb" Output: 2 Explanation: You can swap the first 'a' with the second 'b' and the second 'a' with the fourth 'b'. The string becomes "baab" which is a palindrome.

Example 4:

Input: s = "ntiin" Output: 1 Explanation: You can swap the last 'n' with the second 'i'. The string becomes "nitin" which is a palindrome.

Approach:

  • Check if s is already palindrome or it is not possible to make it palindrome by swapping any two adjacent integers.
  • Create a hashmap to store the last index of each character in the string.
  • Traverse the string and keep track of the longest repeated character substring (LRCS) using two pointers left and right. Calculate the length of the LRCS at each step.
  • If LRCS length is equal to the length of the string s or LRCS length is greater than or equal to half the length of the string s, we can swap adjacent characters in the LRCS to make the string palindrome.
  • If we can make the string palindrome by swapping characters in the LRCS, then we can count the total number of swaps required by iterating through the LRCS and checking which characters need to be swapped.
  • If we can't make the string palindrome by swapping characters in the LRCS, then we can only swap characters outside the LRCS. In this case, we need to calculate the minimum number of swaps required to make the characters outside the LRCS symmetric around the center of the string.

Solution:

class Solution { public: int maxLen = 0; int maxChar = -1; unordered_map<char, int> mp;

int check(string s) {
    int n = s.size();
    // if string is already palindrome
    if (s == string(s.rbegin(), s.rend())) {
        return 0;   
    }
    // if not possible to make palindrome
    if (maxLen >= (n+1)/2) {
        return -1;
    }
    // if possible to make palindrome using LRCS
    int res = 0;
    string tmp = s.substr(maxChar+1, maxLen-2); // LRCS
    for (int i=0, j=tmp.size()-1; i<j; i++, j--) {
        if (tmp[i] != tmp[j]) {
            int l = mp[tmp[i]], r = mp[tmp[j]];
            res += abs(l-r);
            mp[tmp[i]] = r;
            mp[tmp[j]] = l;
        }
    }
    return res;
}

int swapForLongestRepeatedCharSubstr(string s) {
    int n = s.size();
    // check if string is already palindrome
    if (s == string(s.rbegin(), s.rend())) {
        return 0;   
    }
    // create hashmap to store last index of each character
    for (int i=0; i<n; i++) {
        mp[s[i]] = i;
    }
    // calculate longest repeated character substring (LRCS)
    int left = 0, right = 0;
    while (right < n) {
        if (s[right] == s[left]) {
            right++;
        } else {
            if (right - left > maxLen) {
                maxLen = right-left;
                maxChar = left;
            }
            left = right;
        }
    }
    if (n - left > maxLen) {
        maxLen = n-left;
        maxChar = left;
    }
    // check if possible to make palindrome and calculate answer accordingly
    int res = check(s);
    return res;
}

};

Time Complexity: O(n) Space Complexity: O(n)

Swap For Longest Repeated Character Substring Solution Code

1