ICSE And ISC Tutorials By Sandeep Goswami Sir

ICSE And ISC Tutorials By Sandeep Goswami Sir

Share

The purpose of creating this page is only to get Tuition and Coaching free environment. Make the students more confident and flexible.

17/05/2026

Accept a sentence terminated by ‘.’, ‘?’ or ‘!’ only. Sentence may contain any of ()[]{} plus letters/spaces. It must contain no digits and no other symbols besides the final terminator. For any violation, display: INVALID INPUT. Perform the following tasks: (a) Check if the brackets are balanced and properly nested. (b) Print the maximum nesting depth of brackets. (c) If unbalanced, print the index (1-based indexing) of the first error. • A mismatched/extra closing bracket is an error at that bracket’s index. • If brackets are incomplete at the terminator, the first error is the index of the first bracket that cannot be matched (typically where the mismatch first occurs). Test your program for the following data and some random data: Example 1 INPUT: This (is [very {deep}]) indeed! OUTPUT: BALANCED: YES MAX DEPTH: 3 Example 2 INPUT: Simple () test. OUTPUT: BALANCED: YES MAX DEPTH: 1 Example 3 INPUT: Hello world. OUTPUT: BALANCED: YES MAX DEPTH: 0 Example 4 INPUT: We love (balanced [brackets)! OUTPUT: BALANCED: NO ERROR AT INDEX: 22 Example 5 INPUT: Hi ! OUTPUT: INVALID INPUT

SOLUTION:

import java.util.Scanner;
import java.util.Stack;

public class BracketSentenceCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();

if (s == null || s.length() == 0) {
System.out.println("INVALID INPUT");
return;
}

// Check terminator
char term = s.charAt(s.length() - 1);
if (term != '.' && term != '?' && term != '!') {
System.out.println("INVALID INPUT");
return;
}

String body = s.substring(0, s.length() - 1); // exclude terminator

// Validate allowed characters: letters, spaces, and brackets ()[]{}
for (int i = 0; i < body.length(); i++) {
char c = body.charAt(i);
if (Character.isLetter(c) || c == ' '
|| c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}') {
continue;
} else {
System.out.println("INVALID INPUT");
return;
}
}

// Stack for brackets: store pair (char bracket, index 1-based in original sentence)
class Pair {
char ch;
int idx;
Pair(char ch, int idx) { this.ch = ch; this.idx = idx; }
}

Stack st = new Stack();
int maxDepth = 0;
// iterate through original string to get 1-based indices including terminator position
// but only examine positions upto length-1 (body)
for (int i = 0; i < body.length(); i++) {
char c = body.charAt(i);
int pos = i + 1; // 1-based indexing as required
if (c == '(' || c == '[' || c == '{') {
st.push(new Pair(c, pos));
if (st.size() > maxDepth) maxDepth = st.size();
} else if (c == ')' || c == ']' || c == '}') {
if (st.isEmpty()) {
// extra closing bracket -> error at this bracket's index
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + pos);
return;
} else {
Pair top = st.peek();
if (!matches(top.ch, c)) {
// mismatched closing bracket -> error at this bracket's index
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + pos);
return;
} else {
st.pop();
}
}
} else {
// letters/spaces - ignore
}
}

// after processing body, check stack
if (!st.isEmpty()) {
// unmatched opening bracket(s). Error index is the index of the first bracket that cannot be matched.
// That is the position (1-based) of the earliest opening bracket in the stack that stays unmatched.
Pair firstUnmatched = st.firstElement(); // bottom-most unmatched opening bracket (earliest)
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + firstUnmatched.idx);
return;
}

// balanced
System.out.println("BALANCED: YES");
System.out.println("MAX DEPTH: " + maxDepth);
}

private static boolean matches(char open, char close) {
return (open == '(' && close == ')')
|| (open == '[' && close == ']')
|| (open == '{' && close == '}');
}
}

Photos from Ministry of Education's post 26/09/2023
26/09/2023

27 सितम्बर 2023 को ईपीएफओ 2.0 के तहत जिलों में आउटरीच कार्यक्रम आयोजित कर रहा है।

कैंप का स्थान जानने के लिए यहां 👇🏻 क्लिक करेंhttps://www.epfindia.gov.in/site_docs/PDFs/Updates/NAN_Camps_Venue_Details-September%202023%20-%20FINAL%201%20PM.pdf

#ईपीएफ #पीएफ #ईपीएफओ

PMO India Bhupender Yadav BJP Rameswar Teli Ministry of Labour and Employment, Government of India Ministry of Information & Broadcasting, Government of India Press Information Bureau - PIB, Government of India Amrit Mahotsav MyGovIndia

Want your school to be the top-listed School/college in Mirzapur?
Click here to claim your Sponsored Listing.

Telephone

Website

Address


Vindhyachal Road
Mirzapur
231001