Showing posts with label substring. Show all posts
Showing posts with label substring. Show all posts

Tuesday, January 17, 2017

Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

/*
Set an array to record the number of each character in string t. Use two pointers to iterate string s, when the substring in string s contains string t, if the length of the substring is smaller than the previous recorded length, update the minimum window substring. Then move forward the left pointer and continue to check if the new substring contains string t.

time:O(n), space:O(1)
*/
public class Solution {
    public String minWindow(String s, String t) {
        if (s == null || s.length() == 0 || t == null) {
            return "";
        }
        int[] map = new int[256];
        for (char ch : t.toCharArray()) {
            map[ch]++; 
        }
        int i = 0;
        int j = 0;
        int count = t.length();
        int length = Integer.MAX_VALUE;
        String res = "";
        for (i = 0; i < s.length() - t.length() + 1; i++) {
            while (j < s.length() && count > 0) {
                if (map[s.charAt(j)]-- > 0) {
                    count--;
                }
                j++;
            }
            if (count == 0 && j - i < length) {
                length = j - i;
                res = s.substring(i, j);
            }
            if (map[s.charAt(i)]++ == 0) {
                count++;
            }
        }
        return res;
    }
}

Thursday, January 12, 2017

Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.
Example:
Input: "cbbd"

Output: "bb"

/*
There are two conditions, like "acbbc" and "acbcb". The first one's longest palindromic substring is even number and the second one is odd number. We need discuss these two conditions each time to see which one is longer.

time:O(n^2), space:O(1)

*/

public class Solution {
    public String longestPalindrome(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        } 
        String ans = "";
        int maxCount = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            int oddLength = computePalindromeLength(s, i, i);
            int evenLength = computePalindromeLength(s, i, i + 1);
            if (oddLength > evenLength && 2 * oddLength - 1 > maxCount) {
                ans = s.substring(i - oddLength + 1, i + oddLength);
                maxCount = 2 * oddLength - 1;
            } else if (oddLength <= evenLength && 2 * evenLength > maxCount) {
                ans = s.substring(i - evenLength + 1, i + evenLength + 1);
                maxCount = 2 * evenLength;
            }
        }
        return ans;
    }
    public int computePalindromeLength(String s, int left, int right) {
        int count = 0;
        while (left >= 0 && right < s.length()) {
            if (s.charAt(left--) != s.charAt(right++)) {
                break;
            }
            count++;
        }
        return count;
    }
}