Leetcode 365 (2023)

Leetcode 365 (2023)

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

Day 3: 944. Delete Columns to Make Sorted Hi there, Day 3 is here, unfortunately, had to publish this late due to some personal issues. So onto our daily leetcode DSA questions with concise solutions, Now to the Question. You are given an array of n strings strs, all of the same length. The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as: abc bce cae You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted while column 1 ('b', 'c', 'a') is not, so you would delete column 1. Return the number of columns that you will delete. Input: strs = ["cba","daf","ghi"] Output: 1 Explanation: The grid looks as follows: cba daf ghi Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column. Input: strs = ["a","b"] Output: 0 Explanation: The grid looks as follows: a b Column 0 is the only column and is sorted, so you will not delete any columns. Input: strs = ["zyx","wvu","tsr"] Output: 3 Explanation: The grid looks as follows: zyx wvu tsr All 3 columns are not sorted, so you will delete all 3. with constraints given as follows Constraints: n == strs.length 1 <= n <= 100 1 <= strs[i].length <= 1000 strs[i] consists of lowercase English letters. Approach: Given an array of alphabets that should be arranged column-wise to give us an idea of which column is not sorted lexicographically.A lexicographical order simply means an ordered set. Also, take note that the solution is to get the column that is not sorted to be deleted. Typically since the array has to be represented in column format, the first column is initialized to zero to keep track of the number of deletions that need to be made and we are going to iterate through individual columns to get one that is not sorted. /** * @param {string[]} strs * @return {number} */ var minDeletionSize = function(strs) { // initialize the count to be 0 let count = 0; for (let i = 0; i < strs[0].length; i++) { let m = 0; for (let j = 0; j < strs.length-1; j++) { // the charCodeAt method In JavaScript returns an integer btwn 0 and 65535 if (strs[j].charCodeAt(i) > strs[j+1].charCodeAt(i)) m++; } if(m !== 0) count++; } return count; }; from the code above, It shows that you can actually delete the columns that are not sorted lexicographically, in which the function gives you a number as an output So that's all for Day 3, Your comments and optimized solutions to questions are well appreciated. Thank you for reading..

Leetcode 365 (2023)

Leetcode 365 (2023)

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

Day 2: 520. Detect Capital Using JavaScript Hi there, we continue with our daily leetcode DSA questions, And now to Day 2 Question on Detect capital using JavaScript We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode". Only the first letter in this word is capital, like "Google". Given a string word, return true if the usage of capitals in it is right. Examples of test cases are given Input: word = "USA" Output: true Input: word = "FlaG" Output: false 1 <= word.length <= 100 word consists of lowercase and uppercase English letters. Approach I: The method of regular expression(Regex) can be implemented to show if a word has all capital letters, small letters and if the first letter of the word in capital letters. Regex : Regular expressions are patterns used to match character combinations in strings. The regular pattern is defined from the question.The JavaScript code is shown simplify the cases of all capital letters, all small letters and the first word being capital letter /** * @param {string} word * @return {boolean} */ let detectCapitalUse = (word) => { //returns all capital letters, small letter or the first letter being capital letter return /^[A-Z]+$|^[a-z]+$|^[A-Z][a-z]+$/.test(word); }; The function above returns a boolean indicating if the capitalization in the given word holds true. Just like that, you can determine the boolean value from the Regex format that applies to different test cases. Approach II: This makes use of the substring() function in JavaScript which extracts part of a string and outputs a new string, how does this string extract apply to the question? See below var detectCapitalUse = function (word) { return (word.substr(1).toLowerCase() == word.substr(1) || word.toUpperCase() == word) }; Thank you for reading, I'll see you on Day 3.

Leetcode 365 (2023)

Leetcode 365 (2023)

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

Day 1: 290. Word Pattern using JavaScript

392: Is Subsequence (Using Javascript)

392: Is Subsequence (Using Javascript)

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

Hello everyone, my goal to solve DSA questions by giving optimal solutions with detailed explanations will continue with various platforms including Leetcode,I already have a series on hackerrank you could check that out as well. Now to the Problem we have at hand "Is Subsequence" Problem: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not). Example 1: Input: s = "abc", t = "ahbgdc" Output: true Example 2: Input: s = "axc", t = "ahbgdc" Output: false Constraints: 0 <= s.length <= 100 0 <= t.length <= 104 s and t consist only of lowercase English letters. Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? Solution: From the problem above, It is understood that there's a sequence that can be in form of an array, and a subsequence will be formed from the sequence. We'll use Pseudocodes to find a clearer analogy of the process Pseudocode: Initialize the subsequence(s) to be zero(0). Use Comparison operators in Conditional statements to compare values (check the array size). loop through the array index to check if a subsequence is true for all elements. Compare using a triple equals(===) to see if the s value are of similar types or not. Return the subsequence equivalent to the length of the s string( Boolean value: True) JavaScript Code: var isSubsequence = function(s, t) { // initialise the subsequence to be 0 let subsequence = 0; // use comparison operator to check the size of the strings 't' and 's' if (s.length > t.length) return false; //loop through the array index to check if a subsequence is true for all elements for (let i = 0; i < t.length; i++) { // compare using a triple equals to see if the value are of similar types or not if (s[subsequence] === t[i]){ subsequence++; } } return subsequence == s.length; }; Definitely, there are other ways in finding Optimal solutions to this problem.Array methods like slice(), Filter() can also be implemented to achieve great solutions. Let me know what you think about the solution to this problem and ways to make it better. Follow me for more DSA concepts and Step by Step Solutions to Problems. Thank you.

Compare the Triplets

Compare the Triplets

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

Problem Statement Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty. The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]). The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2]. If a[i] > b[i], then Alice is awarded 1 point. If a[i] < b[i], then Bob is awarded 1 point. If a[i] = b[i], then neither person receives a point. Comparison points are the total points a person earned. Given a and b, determine their respective comparison points. Example a = [1, 2, 3] b = [3, 2, 1] For elements 0, Bob is awarded a point because a[0] . For the equal elements a[1] and b[1], no points are earned. Finally, for elements 2, a[2] > b[2] so Alice receives a point. The return array is [1, 1] with Alice's score first and Bob's second. Function Description Complete the function compareTriplets in the editor below. compareTriplets has the following parameter(s): int a[3]: Alice's challenge rating int b[3]: Bob's challenge rating Return int[2]: Alice's score is in the first position, and Bob's score is in the second. Input Format The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a. The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b. Constraints 1 ≤ a[i] ≤ 100 1 ≤ b[i] ≤ 100 Sample Input 0 5 6 7 3 6 10 Sample Output 0 1, 1 Explanation 0 In this example: a = (a[0], a[1], a[2]) = (5,6,7) b = (b[0], b[1], b[2]) = (3,6,10) Now, let's compare each individual score: a[0] > b[0], so Alice receives 1 point. a[1] > b[1], so nobody receives a point. a[2] < b[2], so Bob receives 1 point. Alice's comparison score is 1 and Bob's comparison score is 1. Thus, we return the array [1,1], Sample Input 1 17 28 30 99 16 8 Sample Output 1 2 1 Explanation 1 Comparing the 0th elements, 17 < 99 so Bob receives a point. Comparing the 1st and 2nd elements, 28 > 16 and 30 > 8 so Alice receives two points. The return array is [2,1]. Solution (using Javascript) In order to solve this, let's have a Pseudocode showing the process on how to actualize the final solution. Pseudocode is an informal way of presenting a programming solution. Pseudocode: Initialize Alice and bob score to zero. loop through both arrays (Alice and Bob) and use conditionals to show Alice or Bob ratings Do a comparison of both ratings individually for every loop. After looping, use the comparison keyword (or any you wish to use) to return the comparison array in the format [x,y]. code snippet ( using Javascript) function compareTriplets(a, b) { // initialize the ratings (Alice and Bob score) let aScore = 0; let bScore = 0; //loop through the arrays for (let i = 0; i < 3; i++){ // compare using conditionals (if..else) if (a[i] > b[i]) { aScore = aScore + 1; } else if (a[i] < b[i]) { bScore = bScore + 1; } else { (a[i] == b[i]); 0; } } const comparison = [aScore, bScore]; return comparison; } It is observed that the solution can be solved in some other ways but considering using the loops and the conditional statement is of the essence. We continue with this warmup series on hackerrank as I walk you through easy ways of providing solutions to complex algorithms using Javascript. I will like to read your comments and better solutions. Thank you.

Developer stories

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

I will be updating some of my developer stories soon...Thanks

First time on Hashnode

https://cdn.hashnode.com/res/hashnode/image/upload/v1608929754671/C4qbL8lNo.jpegAkinmegha Temitope Samuel

hi there, I am a Front end Developer with experiences in JavaScript, React, node, a little bit of Angular and a Mobile Application Developer mainly Flutter/Dart Connect with me, I have interesting stuffs to share with this great community and to learn as well.