Posts

Showing posts from December, 2020

Simple XML Validator - The Coding Shala

Home >> Programming Questions >> XML Validator  In this post, we will are going to implement a Simple XML Validator in the Java language. Simple XML Validator in Java The input is an ASCII string, we have to write a Java program to check if the given String is valid XML or not. For simplicity purposes, the XML string only has content and tags.  The output should be a String for whether the text is valid XML or not. If the XML is invalid, output one of the three error strings: 1. "missing closing tag for <start_tag>" 2. "encountered closing tag without matching open tag for </end tag>" 3. "parse error" Example 1: Input: <a>some text</a> Output: valid Example 2: Input: <a> Output: missing closing tag for <a> Approach We can use the stack data structure. Java Program: class Solution { public String validate_xml ( String xml ) { if ( xml == null || xml . isEmpty ()) return "v...

XML vs JSON - The Coding Shala

Home >> CS Fundamentals >> XML vs JSON  In this post, we will learn the basic difference between XML and JSON. XML vs JSON XML and JSON are the most popular formats of data that can be understood both by computers and by humans. These formats are used to exchange information between servers, between various components in a single application, and much more. XML XML stands for eXtensible Markup Language. XML uses tags to hold data. Everything in XML is built on tags. A tag is a specially-formatted label used in markup languages: <opening_tag>content</closing tag>. XML is used for storing and transporting data. XML contains data in a hierarchical order. Example of XML: < ROOT > < PARENT > < CHILD1 ></ CHILD1 > < CHILD2 ></ CHILD > </ PARENT > </ ROOT > < bookstore > < book category = "some categoty" > < title lang = "en" > Everyday Ita...

LeetCode - Valid Square Solution - The Coding Shala

Home >> LeetCode >> Valid Square In this post, We will learn how to solve LeetCode's Valid Square problem and will implement a Valid Square solution in Java. Valid Square Given the coordinates of four points in 2D space, return whether the four points could construct a square.  The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Solve this problem on LeetCode: Click Here Valid Square Java Solution Approach We will find 4 sides and 2 diagonals length and for a valid square all 4 sides should be the same length and both diagonal also should be the same length and side and diagonal length is not the same. Java Program: class Solution { public boolean validSquare ( int [] p1 , int [] p2 , int [] p3 , int [] p4 ) { int [] lengths = { length ( p1 , p2 ), length ( p1 , p3 ), length ( p1 , p4 ), length ( p2 , p3 ), length ...

How to find Second largest element in an Array - The Coding Shala

Home >> Interview Questions >> Second Largest Element in Array  In this post, we will learn how to Find Second Largest Element in an Array and will implement its code in Java. Second Largest Element in Java Given an array. You have to find the second largest element in the array. Approach First, we can find the largest element in the array then the second-largest, or we can do the same steps in a single loop. Java Program: class A { void findMax ( Integer [] arr ) { if ( arr . length < 2 ) { System . out . println ( "Invalid Input" ); return ; } Integer first = Integer . MIN_VALUE ; Integer second = first ; for ( Integer num : arr ) { if ( num > first ) { second = first ; first = num ; } else if ( num > second && num != first ) { second = num ; } } System . out . println ( "First big is: " + first ); if ( second == Integer . M...

How to Reverse a String in Java - The Coding Shala

Home >> Java Programs >> Reverse a String In this post, we will learn how to Reverse a String in Java. Reverse a String in Java Given a string S as input. You have to reverse the given string. Example: Input: 3 Geeks GeeksforGeeks GeeksQuiz Output: skeeG skeeGrofskeeG ziuQskeeG Approach 1 Using StringBuilder. Java Program: import java.util.* ; import java.lang.* ; import java.io.* ; class GFG { public static void main ( String [] args ) { //code Scanner sc = new Scanner ( System . in ); int tc = sc . nextInt (); while ( tc -- > 0 ) { String str = sc . next (); StringBuilder sb = new StringBuilder ( str ); sb . reverse (); System . out . println ( sb . toString ()); } } } Approach 2 By using for loop from end to start. Java Program: import java.lang.* ; import java.io.* ; class GFG { public static void main ( String [] args ) { //code Scanner sc = new Scann...