Skip to main content

Posts

Showing posts from May, 2022

Simple Text Editor HackerRank Solution in Java with Explanation

Simple Text Editor HackerRank Solution in Java with Explanation Problem Description : Implement a simple text editor. The editor initially contains an empty string S. Perform Q operations of the following 4 types: append (W) - Append string W to the end of S. delete (k) - Delete the last K characters of S. print (k) - Print the kth character of S. undo () - Undo the last (not previously undone) operation of type 1 or 2, reverting S to the state it was in prior to that operation. Example : Input : S = "abcde" Operations = ["1 fg", "3 6", "2 5", "4", "3 7", "4", "3 4"] index   S          ops[index]    explanation -----   ------     ----------    ----------- 0       abcde      1 fg          append fg 1       abcdefg    3 6        ...

Balanced Brackets HackerRank Solution in Java and Python with Explanation

Java and Python Solution for Balanced Brackets HackerRank Problem | Check opening and closing brackets match or not Problem Description : A bracket is considered to be any one of the following characters: ( , ) , { , } , [ , or ] . Two brackets are considered to be a matched pair if the an opening bracket (i.e., ( , [ , or { ) occurs to the left of a closing bracket (i.e., ) , ] , or } ) of the exact same type . There are three types of matched pairs of brackets: [] , {} , and () . A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {[(])} is not balanced because the contents in between { and } are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket, ( , and the pair of parentheses encloses a single, unbalanced closing square bracket, ] . By this logic, we say a sequence of brackets is balanced if the following conditions are met: It contains no unmatched brackets. The subset of bra...

Recursive Digit Sum HackerRank solution in Java with Explanation

Java solution with explanation for Recursive Digit Sum HackerRank problem Problem Description : We define super digit of an integer x using the following rules:  Given an integer, we need to find the super digit of the integer. If x has only 1 digit, then its super digit is x. Otherwise, the super digit of x is equal to the super digit of the sum of the digits of x. For example, the super digit of 9875 will be calculated as: super_digit(9875)      9 + 8 + 7 + 5 = 29 super_digit(29)          2 + 9 = 11 super_digit(11)          1 + 1 = 2 super_digit(2)             2 Example 1 : n = 9875 k = 4 The number p is created by concatenating the string n, k times so the initial p = 9875987598759875. superDigit(p) = superDigit(9875987598759875)     9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116 superDigit(p) = superDigit(116)     1+1+...

Why we must declare immutable class as Final? Why is an immutable class supposed to be final?

Why immutable class declared as final in Java? Immutable class means, once object is created it can not be changed.  Learn more about Immutable class in details : Create Immutable Class in Java | Advantages of immutable class in java   Now lets see why we must declare immutable class as final in Java. If immutable class does not defined as final then any other class can extends that class and change properties of that class and broke the objective of immutability. So final class is important in immutability. Lets see using example. Programming.java public class Programming {          private final String name;          public Programming(String name) {         this.name = name;     }     public String getName() {         return name;     }     @Override     public String toString() { ...