Count Triplets That Can Form Two Arrays of Equal XOR

Problem Given an array of integers arr. We want to select three indices i, j and k where (0 <= i < j <= k < arr.length). Let’s define a and b as follows: a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1] b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k] Note that ^ denotes the bitwise-xor operation. ...

Reverse words in a string keep delimiters in order

Problem Given a string and a set of delimiters, reverse the words in the string while maintaining the relative order of the delimiters. For example, given “hello/world:here”, return “here/world:hello” Follow-up Does your solution work for the following cases: "hello/world:here/", "hello//world:here" Examples Example 1: Input: s = hello/world:here Output: here/world:hello Solution Method 1 - Using stack to store words and queue for delimiters Code Java class Solution { public String reverseWords(String s) { Stack<String> stack = new Stack<>(); Queue<String> queue = new LinkedList<>(); StringBuilder word = new StringBuilder(); int i = 0; while (i < s.length()) { char c = s.charAt(i); if (Character.isLetterOrDigit(c)) { word.append(c); i++; } else { if (word.length() > 0) { stack.push(word.toString()); } word = new StringBuilder(); // clear word for next word StringBuilder delim = new StringBuilder(); delim.append(c); i++; // tracking continuous delimiter to add to queue while (i < s.length() && !Character.isLetterOrDigit(s.charAt(i))) { delim.append(c); i++; } queue.add(delim.toString()); } } // if string ends with words if (word.length() != 0) { stack.push(word.toString()); } boolean isWordFirst = Character.isLetterOrDigit(s.charAt(0)); StringBuilder sb = new StringBuilder(); // if word is not first, add 1 entry from queue if (!isWordFirst) { if (!queue.isEmpty()) { sb.append(queue.remove()); } } // now put 1 element from either till not empty while (!stack.isEmpty() || !queue.isEmpty()) { if (!stack.isEmpty()) { sb.append(stack.pop()); } if (!queue.isEmpty()) { sb.append(queue.remove()); } } return sb.toString(); } } ...

Number of Steps to Reduce a Number in Binary Representation to One

Problem Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules: If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it. It is guaranteed that you can always reach one for all test cases. Examples Example 1: ...

Get Equal Substrings Within Budget

Problem You are given two strings s and t of the same length and an integer maxCost. You want to change s to t. Changing the ith character of s to ith character of t costs |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters). Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost. If there is no substring from s that can be changed to its corresponding substring from t, return 0. ...

Special Array With X Elements Greater Than or Equal X

Problem You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x. Notice that x does not have to be an element in nums. Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique. ...

Student Attendance Record 2 Problem

Problem An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters: 'A': Absent. 'L': Late. 'P': Present. Any student is eligible for an attendance award if they meet both of the following criteria: The student was absent ('A') for strictly fewer than 2 days total. The student was never late ('L') for 3 or more consecutive days. Given an integer n, return the number of possible attendance records of length n that make a student eligible for an attendance award. The answer may be very large, so return it modulo 10^9 + 7. ...

Student Attendance Record 1 Problem

Problem You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters: 'A': Absent. 'L': Late. 'P': Present. The student is eligible for an attendance award if they meet both of the following criteria: The student was absent ('A') for strictly fewer than 2 days total. The student was never late ('L') for 3 or more consecutive days. Return true if the student is eligible for an attendance award, or false otherwise. ...

Maximum Score Words Formed by Letters Problem

Problem Given a list of words, list of single letters (might be repeating) and score of every character. Return the maximum score of any valid set of words formed by using the given letters (words[i] cannot be used two or more times). It is not necessary to use all characters in letters and each letter can only be used once. Score of letters 'a', 'b', 'c', … ,'z' is given by score[0], score[1], … , score[25] respectively. ...

The Number of Beautiful Subsets Problem

Problem You are given an array nums of positive integers and a positive integer k. A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k. Return the number of non-empty beautiful subsets of the array nums. A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices to delete are different. ...

Sum of All Subset XOR Totals

Problem The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty. For example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1. Given an array nums, return the sum of all XOR totals for every subset of nums. Note: Subsets with the same elements should be counted multiple times. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy