Skip to main content

Posts

Daily Coding Problem: Validate a Bracket String

Problem A string contains only (), [] and {}. Return whether every opening bracket closes in reverse order. Worked example {[()]} is valid. ([)] fails when ) meets [. Read {, [, and ( in that order. The most recent opener must close first, so ) removes (, ] removes [, and } removes {. For ([)], the first closer is ) while [ is still on top. Stop immediately. How the solution works The stack holds unmatched openers, with the most recent one on top. An empty stack at the end means no opening bracket remains. Python stores opening brackets. Java stores the closing bracket it expects, so the two versions enforce the same order. Counting opening and closing brackets alone misses crossed pairs like ([)]. Python solution def valid_brackets(text): closing = {')': '(', ']': '[', '}': '{'} stack = [] for char in text: if char not in closing: stack.append(char) elif not stack or stack.pop() != closing[char]: ...
Recent posts

Daily Coding Problem: Find Two Numbers That Add to a Target

Problem Given a list of integers and a target, return two distinct indices whose values add to the target. Return None in Python or null in Java if no pair exists. Worked example For [4, 1, 6, 2] with target 8, return (2, 3). Insert 4 at index 0 and 1 at index 1. At index 2, 6 needs 2, which has not appeared yet. At index 3, 2 needs 6, and the map returns index 2. How the solution works The map contains only earlier indices, so the first match gives two distinct indices. Every pair can be found when the algorithm visits its later index. Checking the map before insertion prevents a single 4 from being used twice when the target is 8. The Java version uses long for the complement so integer overflow cannot produce a false match. Python solution def two_sum(nums, target): seen = {} for index, value in enumerate(nums): complement = target - value if complement in seen: return seen[complement], index seen[value] = index return None Java soluti...

Mastering Zigzag Conversion: Transforming Text with Java and Python

Zigzag Conversion is a fascinating text transformation technique that adds visual appeal and complexity to encoded text. If you're a Java or Python enthusiast looking to explore this algorithm, you've come to the right place! In this blog post, we'll guide you through implementing Zigzag Conversion using Java and Python, providing you with the knowledge to transform text in a captivating zigzag pattern.   Understanding the Zigzag Conversion: The Zigzag Conversion algorithm is designed to rearrange characters in a string, creating a visually appealing zigzag pattern. It achieves this by organizing the characters in rows, following a specific pattern of upward and downward movements. The resulting transformed text can be an intriguing visual representation of the original input. How Does Zigzag Conversion Work? Let's dive into the mechanics of Zigzag Conversion. The algorithm takes two parameters: the input string and the number of rows in the zigzag pattern. It th...

Longest Palindromic Substring: Explained with Java and Python Code

The Longest Palindromic Substring problem is a classic challenge in the realm of string manipulation. In this blog, we will delve into the intricacies of finding the longest palindromic substring in a given string. Through a step-by-step approach and illustrative examples, you will gain a comprehensive understanding of the problem and learn how to solve it efficiently.   Understanding the Problem: A palindrome is a string that reads the same forward and backward. The Longest Palindromic Substring problem requires finding the longest substring within a given string that is also a palindrome. Our goal is to determine the length of this substring and return the substring itself.   Approach: To solve the Longest Palindromic Substring problem, we can employ a dynamic programming approach or use the expanding around center technique. Here, we will explore the dynamic programming approach. Create a boolean table, dp, of size [n][n] (where n is the length of the input string) to store ...

Mastering the Lowest Common Ancestor of a Binary Search Tree: Implementations in Java and Python

The Lowest Common Ancestor (LCA) of a Binary Search Tree (BST) is a fascinating problem that often arises in the domain of data structures and algorithms. In this blog, we will explore the intricacies of finding the LCA of a BST and provide a step-by-step solution. By understanding the underlying concepts and examining implementation examples, you will gain the knowledge and skills needed to tackle this problem with confidence.   Understanding the Problem: Given a Binary Search Tree and two nodes, our goal is to find their lowest common ancestor. The LCA is the deepest node in the BST that is an ancestor of both given nodes. It represents the shared parent node of the two nodes in the BST hierarchy.   Approach: To find the LCA of two nodes in a BST, we can leverage the characteristics of a BST and utilize a recursive approach. The key steps of the algorithm are as follows: Start from the root of the BST. Compare the values of the current node with the values of the two given n...