Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Saturday, January 14, 2017

Check Valid Parentheses

Given a string only with '(' and ')'. If it is a valid parentheses string, return the number of valid parentheses pairs. One '(' and one ')' is one pair. Otherwise, return -1. 

import java.util.*;

public class Solution {
    public static int validParentheses(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == '(') {
                stack.push('(');
            } else if (word.charAt(i) == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return -1;
                } 
            } else {
                return -1;
            }
        }
        if (!stack.isEmpty()) {
            return -1;
        }
        return word.length() / 2;
    }

    public static void main(String[] args) {
        int res = validParentheses("(())");
        System.out.println(res);
    }
}

Monday, January 9, 2017

Implement strStr()

Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

/*
Traverse all the possible starting points of haystack (from 0 to haystack.length() - needle.length()) and see if the following characters in haystack match those in needle.

time:O(m*n), space:O(1)
bonus: KMP, time:O(n),space:O(1)
*/

public class Solution {
    public int strStr(String haystack, String needle) {
        if (haystack == null || needle == null) {
            return -1;
        }
        int h = haystack.length();
        int n = needle.length();
        for (int i = 0; i < h - n + 1; i++) {
            int j = 0;
            for (j = 0; j < n; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    break;
                }
            }
            if (j == n) {
                return i;
            }
        }
        return -1;
    }
}

Saturday, January 7, 2017

Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.

/*
This problem is like given an array that satisfies the specific rule transformation, and compute the nth element.

Generate the new sequence based on the current one with two steps.
1. Iterate the current sequence and stop when the later character is different from the previous one and record the times of the previous character.
2. Continue the loop until all the characters and their numbers are recorded.

time:O(n * m), space:O(1), m is the average length of n sequences.
*/

public class Solution {
    public String countAndSay(int n) {
        if (n <= 0) {
            return "";
        }
        String ans = "1";
        for (int i = 1; i < n; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < ans.length(); j++) {
                int count = 1;
                while (j + 1 < ans.length() && ans.charAt(j + 1) ==                        ans.charAt(j)) {
                    j++;
                    count++;
                }
                sb.append(String.valueOf(count) +                                             String.valueOf(ans.charAt(j)));
            }
            ans = sb.toString();
        }
        return ans;
    }
}