字符串之括号的有效性

74 阅读1分钟

题目:

给定一个字符串str,判断是不是整体有效的括号字符串

举例:

str = "()" return true; stre = "()()" return true;
str = "())" return false; str = "()a()" return false;


\

代码:

package com.chenyu.string.cn;

public class IsValid {
	public static void main(String[] args) {
		String s1 = "()";
		String s2 = "()(()";
		String s3 = "()(4)";
		System.out.println("result1:" + isValid(s1));
		System.out.println("result2:" + isValid(s2));
		System.out.println("result3:" + isValid(s3));
		
	}
	
	public static boolean isValid(String s) {
		if (s == null || "".equals(s)) {
			return false;
		}
		char[] chars =  s.toCharArray();
		int count = 0;
		for (int i = 0; i < chars.length; i++) {
			i