Java : A string consists of parenthesis and letters. Write a program to validate all the parenthesis.


import java.util.*;
/*
*
* A string consists of parenthesis and letters. Write a program to validate all the parenthesis.
* Ignore the letters.eg.
* ((alf)ls) – valid
* )(dkk)() – invalid
*/

public class StringI_1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter string :- ");
String str = sc.nextLine();

System.out.println("Entered string is :- " + str);

int countOpen = 0 ;
int countClose =0 ;
int sizeOfString = str.length();

if(str == null || sizeOfString == 0){
System.out.println("String is Empty");
System.exit(0);
}

if (str.charAt(0) != '(' && str.charAt(sizeOfString - 1) != ')' ){
System.out.println("First : Invalid String");
System.exit(0);

}
/*else
System.out.println("valid");*/

for(int i = 0 ; i< sizeOfString ; i++){

if(str.charAt(i) == '(' && i!=(sizeOfString-1) ){
if( str.charAt(i+1) == ')' ){
System.out.println("Second :Invalid String ");
System.exit(0);
}else{
countOpen++;
System.out.println("countOpen :- " + countOpen +" "+ str.charAt(i));
}
}else {
if(str.charAt(i) == ')' ){ //&& str.charAt(i+1) !=')'
countClose++;
System.out.println("countClose :- " + countClose +" "+ str.charAt(i));
}
}
}
if(countOpen == countClose){
System.out.println("Valid string");
}else{
System.out.println("Third : Invalid String");
}
}

}

2 comments:

  1. Below is the solution to validate the paranthesis :

    /* A string consists of parenthesis and letters. Write a program to validate all the parenthesis. Ignore the letters.
    eg. ((alf)ls) – valid
    )(dkk)() – invalid*/
    package com.java.DemoPractice;

    import java.util.HashMap;
    import java.util.Map.Entry;

    public class Test2 {


    public static void main(String ar[])
    {
    HashMap hmap=new HashMap();
    String str=")(dkk)()";
    char[] ch=str.toCharArray();
    for(char ch1:ch)
    {
    if(hmap.containsKey(ch1))
    hmap.put(ch1, hmap.get(ch1)+1);
    else
    hmap.put(ch1, 1);
    }
    System.out.println(hmap);
    Integer i = null,i1 = null;
    for(Entry entry :hmap.entrySet())
    {
    if(entry.getKey()=='(' )
    {
    i=entry.getValue();

    System.out.println(entry.getKey()+ " " +i);

    }
    if(entry.getKey()==')' )
    {
    i1=entry.getValue();

    System.out.println(entry.getKey()+ " " +i1);

    }



    }
    if(i==i1)
    System.out.println("String is valid");
    else
    System.out.println("String is not valid");


    }
    }

    ReplyDelete
  2. package pratice;

    public class StringWithBracket {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    checkBracketCount("()(dkk)()");

    }

    static void checkBracketCount(String s){

    int[] letters = new int[128];
    char[] charArray = s.toCharArray();

    for(char c: charArray){
    letters[c]++;
    }

    char openBracket = '(';
    char closeBracket =')';
    int openBracketCount = letters[openBracket];
    int closeBracketCount = letters[closeBracket];

    if(openBracketCount == closeBracketCount){
    System.out.println("Valid String");
    }
    else{
    System.out.println("Invalid String");
    }


    }

    }

    ReplyDelete

Android : How to connect your Android device over Wifi using ADB command for App debugging

How to connect your Android device over Wi-Fi using ADB command Sometimes it requires to connect your Android dev...