package lintcode.string;

/**
 */
public class Solution {

    // 8-Rotate String
    public void rotateString(char[] str, int offset) {
        if (str.length == 0) return;
        int n = str.length;
        offset %= n;
        swap(str, 0, n - 1);
        swap(str, 0, offset - 1);
        swap(str, offset, n - 1);
    }

    private void swap(char[] str, int i, int j) {
        for (; i < j; i++, j--) {
            char tmp = str[i];
            str[i] = str[j];
            str[j] = tmp;
        }
    }

    // 157-Unique Character
    public boolean isUnique(String str) {
        if (str.length() > 256) return false;

        boolean[] exist = new boolean[256];
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (exist[c]) return false;
            exist[c] = true;
        }
        return true;
    }

    // 397-Longest Increasing Continuous Subsequence
    public int longestIncreasingContinuousSubsequence(int[] A) {
        if (A.length == 0) return 0;
        int maxinc = 1, maxdec = 1;
        for (int i = 1, inc = 1, dec = 1; i < A.length; i++) {
            if (A[i - 1] == A[i]) inc = dec = 1;
            else if (A[i - 1] < A[i]) {
                inc++;
                dec = 1;
            } else {
                dec++;
                inc = 1;
            }
            maxinc = Math.max(maxinc, inc);
            maxdec = Math.max(maxdec, dec);
        }
        return Math.max(maxinc, maxdec);
    }

}

@@ -856,24 +856,438 @@
 rue;%0A    %7D%0A%0A
+    // O(1) space solution if we can assume only lowercase letter is allowed%0A    public boolean isUnique_o1(String str) %7B%0A        int exist = 0; // assume only lowercase char that an int can hold%0A        for (int i = 0; i %3C str.length(); i++) %7B%0A            int idx = str.charAt(i) - 'a';%0A            if (((exist %3E%3E idx) & 1) == 1) return false;%0A            exist %7C= 1 %3C%3C idx;%0A        %7D%0A        return true;%0A    %7D%0A%0A
     // 397-L

first unique character in a string