www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Valid Parentheses

Last update: 1/6/2017 8:58:00 AM

By Fatih KABAKCI

The problem is to find out if the input are composed by valid parentheses. The opening and closing brackets must be closed in the correct way.

Problem #20 - Valid Parentheses

Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.ValidParentheses;

import java.util.LinkedList;

/**
 * @author fkabakci
 * 
 * Problem Description: Given a string containing just the characters "(", ")", "{", "}", "[" and "]", 
 * determine if the input string is valid. The brackets must close in the correct order, 
 * "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
 * 
 * Solution:
 * 
 * Go to first closing bracket, check whether the previous one is the correct opening tag. If so, remove those characters from the list,
 * until the list is empty.
 * 
 * 1. Put all characters to a collection list.
 * 2. While i : 0 to size of list
 * 	  if the character is one of the closing brackets - ")", "}", "]"
 * 		 a. Find out the conjugate one - "(", "{", "["
 * 		 b. Get the just previous character of current i in the list.
 * 		 c. Check them out if they are equal
 * 		 d. Remove both characters from the list.
 * 3. At the end, list should be empty if the matches are valid.
 */

public class ValidParentheses {
	public char conjugate(char c) {
		if (c == ")")
			return "(";
		else if (c == "]")
			return "[";
		else
			return "{";
	}

	public boolean isValid(String s) {
		LinkedList<Character> o = new LinkedList<Character>();
		for (char bracket : s.toCharArray()) {
			o.add(bracket);
		}

		int i = 0;
		while (i < o.size()) {
			char c = o.get(i);
			if (c == ")" || c == "}" || c == "]") {
				char _c = conjugate(c);
				char pc = (i > 0) ? o.get(i - 1) : "\0";
				if (pc != _c)
					return false;
				o.remove(i);
				o.remove(i - 1);
				i = i - 2;
			}
			i++;
		}
		return o.isEmpty();
	}

	public static void main(String[] args) {
		ValidParentheses v = new ValidParentheses();
		System.out.println(v.isValid("]"));
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.