LeetCode – Repeated String Match (Java)

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1. For example, with A = “abcd” and B = “cdabcdab”. Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of … Read more

LeetCode – Permutation in String (Java)

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. For example: Input: s1 = “ab” s2 = “eidbaooo” Output: True Explanation: s2 contains one permutation of s1 (“ba”). Java … Read more

Evaluate math expression with plus, minus and parentheses (java)

Given a string of math expression, such as 1-(2+3), evaluate the value. The expression contains only digits, +, – and parentheses. Java Solution import java.util.ArrayList; import java.util.Stack;   public class ExpressionEvaluator { static class Node { Boolean isPositive; Integer value; ArrayList<Node> list;   public Node(boolean isPositive, Integer value) { this.isPositive = isPositive; this.value = value; … Read more

LeetCode – Backspace String Compare (Java)

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = “ab#c”, T = “ad#c” Output: true Explanation: Both S and T become “ac”. Example 2: Input: S = “a##c”, T = “#a#c” Output: true Explanation: Both … Read more