<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ben Pereira</title>
    <description>The latest articles on DEV Community by Ben Pereira (@bendlmp).</description>
    <link>https://dev.to/bendlmp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1141455%2Fe9907363-01af-4124-8599-690d96a9b79d.jpeg</url>
      <title>DEV Community: Ben Pereira</title>
      <link>https://dev.to/bendlmp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bendlmp"/>
    <language>en</language>
    <item>
      <title>LeetCode — 1791. Find Center of Star Graph</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Mon, 14 Jul 2025 14:58:57 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-1791-find-center-of-star-graph-f47</link>
      <guid>https://dev.to/bendlmp/leetcode-1791-find-center-of-star-graph-f47</guid>
      <description>&lt;p&gt;It’s an easy problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.&lt;/p&gt;

&lt;p&gt;You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first thing before starting to solve this issue is to understand a star graph, best way to get into it is looking into different types:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febxcrnchdrfxojkviqxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febxcrnchdrfxojkviqxp.png" alt=" " width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in this image, the idea of the star graph is that there is multiple points and a center piece. With that in mind we can always assume that the second array in the edges will be the one related to the center, but the exact number is still something that we need to identify based on validations.&lt;/p&gt;

&lt;p&gt;With that in mind then is just about comparing the numbers and giving the right one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int findCenter(int[][] edges) {
        return edges[0][1] == edges[1][0] || edges[0][1] == edges[1][1] ? edges[0][1] : edges[0][0];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 0ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 72.20 MB, less than 61.83% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If for some reason the items were mixed then would be a bit more trouble to look for the center piece and compare with the respective one (meaning we would have to iterate it further), but that’s not the case here.&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>arrays</category>
      <category>graphs</category>
    </item>
    <item>
      <title>LeetCode — 3264. Final Array State After K Multiplication Operations I</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Mon, 07 Jul 2025 15:07:28 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-3264-final-array-state-after-k-multiplication-operations-i-26bh</link>
      <guid>https://dev.to/bendlmp/leetcode-3264-final-array-state-after-k-multiplication-operations-i-26bh</guid>
      <description>&lt;p&gt;It’s an easy problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given an integer array nums, an integer k, and an integer multiplier.&lt;/p&gt;

&lt;p&gt;You need to perform k operations on nums. In each operation:&lt;/p&gt;

&lt;p&gt;Find the minimum value x in nums. If there are multiple occurrences of the minimum value, select the one that appears first.&lt;/p&gt;

&lt;p&gt;Replace the selected minimum value x with x * multiplier.&lt;/p&gt;

&lt;p&gt;Return an integer array denoting the final state of nums after performing all k operations.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: nums = [2,1,3,5,6], k = 5, multiplier = 2&lt;/p&gt;

&lt;p&gt;Output: [8,4,6,5,6]&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;OperationResultAfter operation 1[2, 2, 3, 5, 6]After operation 2[4, 2, 3, 5, 6]After operation 3[4, 4, 3, 5, 6]After operation 4[4, 4, 6, 5, 6]After operation 5[8, 4, 6, 5, 6]&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;Input: nums = [1,2], k = 3, multiplier = 4&lt;/p&gt;

&lt;p&gt;Output: [16,8]&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;OperationResultAfter operation 1[4, 2]After operation 2[4, 8]After operation 3[16, 8]&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= nums[i] &amp;lt;= 100&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= k &amp;lt;= 10&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= multiplier &amp;lt;= 5]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, for this problem we can separate into three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Iterate k times&lt;/li&gt;
&lt;li&gt;Find the smallest number in the list&lt;/li&gt;
&lt;li&gt;Update smallest number with multiplier&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s also separate each into a method so it’s easier to understand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] getFinalState(int[] nums, int k, int multiplier) {
        for(int i=0;i&amp;lt;k;i++) { // iterate k operations
            final int smallestIndex = smallestNumberIndex(nums);
            nums[smallestIndex] = applyMultiplier(nums[smallestIndex], multiplier);
        }

        return nums;
    }

    public int smallestNumberIndex(final int[] nums) {
        int index = 0;
        int x = nums[index];
        for(int i=1;i&amp;lt;nums.length;i++) {
            if(x &amp;gt; nums[i]) {
                index = i;
                x = nums[i];
            }
        }
        return index;
    }

    public int applyMultiplier(final int x, final int multiplier) {
        return x * multiplier;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 66.48% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 44.84 MB, less than 72.37% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>array</category>
      <category>numbers</category>
    </item>
    <item>
      <title>LeetCode — 2373. Largest Local Values in a Matrix</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Fri, 06 Jun 2025 12:30:04 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2373-largest-local-values-in-a-matrix-28cj</link>
      <guid>https://dev.to/bendlmp/leetcode-2373-largest-local-values-in-a-matrix-28cj</guid>
      <description>&lt;p&gt;This is an easy problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given an n x n integer matrix grid.&lt;/p&gt;

&lt;p&gt;Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:&lt;/p&gt;

&lt;p&gt;maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.&lt;/p&gt;

&lt;p&gt;In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.&lt;/p&gt;

&lt;p&gt;Return the generated matrix.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As the problem states, the result needs to be a smaller matrix where each item should be the largest among it’s neighbors. With that in mind, there isn’t much to do besides following the conditions, which is traverse the grid, on each variable check it’s neighbors and get the largest.&lt;/p&gt;

&lt;p&gt;The result would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[][] largestLocal(int[][] grid) {
        final int [][] response = new int[grid.length - 2][grid[0].length - 2];

        for(int i=1 ; i &amp;lt; (grid.length-1) ; i++) {
            for(int j=1; j &amp;lt; (grid[i].length-1) ; j++) {
                response[i-1][j-1] = findLargestAmontNeighbors(grid, i, j);
            }
        }

        return response;
    }

    public int findLargestAmontNeighbors(final int[][] grid, final int i, final int j) {
        int largest = 0;
        for(int a=-1 ; a &amp;lt; 2 ; a++) {
            for(int b=-1 ; b &amp;lt; 2 ; b++) {
                if (largest &amp;lt; grid[i+a][j+b]) {
                    largest = grid[i+a][j+b];
                }
            }   
        }
        return largest;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 3ms, faster than 66.48% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 45.72 MB, less than 38.80% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A simple improvement here that can be done would be when defining a and b use the indexes already instead of calculate later, but that’s a very small one.&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>matrix</category>
      <category>numbers</category>
    </item>
    <item>
      <title>LeetCode — 2824. Count Pairs Whose Sum is Less than Target</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Fri, 30 May 2025 13:51:14 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2824-count-pairs-whose-sum-is-less-than-target-380</link>
      <guid>https://dev.to/bendlmp/leetcode-2824-count-pairs-whose-sum-is-less-than-target-380</guid>
      <description>&lt;p&gt;It’s an easy problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 &amp;lt;= i &amp;lt; j &amp;lt; n and nums[i] + nums[j] &amp;lt; target.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s a easy problem so we shouldn’t go deep on it. First we read and understand the conditions, and with that we see that we may need to iterate twice nums (it’s possible to not, but add much more pain than purpose), and the condition that the sums needs to be smaller than the target. With that in mind, let’s jump straight to the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int countPairs(List&amp;lt;Integer&amp;gt; nums, int target) {
        int pairs = 0;

        for(int i=0;i&amp;lt;nums.size();i++) {
            for(int j=(i+1);j&amp;lt;nums.size();j++) {
                if((nums.get(i) + nums.get(j)) &amp;lt; target) {
                    pairs++;
                }
            }
        }

        return pairs;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 2ms, faster than 96.64% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage:42.23 MB, less than 83.37% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see, is as simple as it gets. That’s the code translated from the problem as solution.&lt;/p&gt;




&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>numbers</category>
      <category>pairs</category>
    </item>
    <item>
      <title>LeetCode — 2798. Number of Employees Who Met the Target</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Thu, 29 May 2025 12:57:09 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2798-number-of-employees-who-met-the-target-2mpa</link>
      <guid>https://dev.to/bendlmp/leetcode-2798-number-of-employees-who-met-the-target-2mpa</guid>
      <description>&lt;p&gt;It’s an easy problem with description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company.&lt;/p&gt;

&lt;p&gt;The company requires each employee to work for at least target hours.&lt;/p&gt;

&lt;p&gt;You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target.&lt;/p&gt;

&lt;p&gt;Return the integer denoting the number of employees who worked at least target hours.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This problem is simple and straight forward, you basicaly iterate the array of hours, the ones that are equal or bigger than the target you add into a sum, and then return the sum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
        int employeesWhoMetTarget = 0;

        for(int i=0;i&amp;lt;hours.length;i++){
            if(hours[i] &amp;gt;= target) {
                employeesWhoMetTarget++;
            }
        }

        return employeesWhoMetTarget;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 0ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 43.08 MB, less than 9.90% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;if you want to do a bit more fancy you can do like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
       return (int) Arrays.stream(hours)
                         .filter(employeeHours -&amp;gt; employeeHours &amp;gt;= target)
                         .count();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 5ms, faster than 1.24% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 42.53 MB, less than 35.86% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looks nice, but not as good as the first one, if you need performance go for the one that doesn’t use stream.&lt;/p&gt;




&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>interview</category>
      <category>leetcode</category>
      <category>iteration</category>
    </item>
    <item>
      <title>LeetCode — 3467. Transform Array by Parity</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Wed, 28 May 2025 18:17:16 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-3467-transform-array-by-parity-25d1</link>
      <guid>https://dev.to/bendlmp/leetcode-3467-transform-array-by-parity-25d1</guid>
      <description>&lt;p&gt;This is an easy problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given an integer array nums. Transform nums by performing the following operations in the exact order specified:&lt;/p&gt;

&lt;p&gt;Replace each even number with 0.&lt;/p&gt;

&lt;p&gt;Replace each odd numbers with 1.&lt;/p&gt;

&lt;p&gt;Sort the modified array in non-decreasing order.&lt;/p&gt;

&lt;p&gt;Return the resulting array after performing these operations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is two ways of getting it solved. First and as simple as can be, you iterate, identify and replace the numbers with 0 and 1 if even or odd and then you sort your array before returning it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] transformArray(int[] nums) {
        final int[] response = replaceEvenWithZeroOddWithOne(nums);
        Arrays.sort(response);
        return response;
    }

    public boolean isEven(final int num) {
        return num % 2 == 0; // &amp;amp; can be used as well
    }

    public int[] replaceEvenWithZeroOddWithOne(final int[] nums) {
        final int[] response = new int[nums.length];
        for(int i=0;i&amp;lt;nums.length;i++){
            response[i] = isEven(nums[i]) ? 0 : 1;
        }
        return response;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 2ms, faster than 59.68% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 44.70 MB, less than 95.92% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;is not bad, and you can reuse the methods there, but there is a way to do everything inside a for loop, that would be using two pointers for the even and the odd:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] transformArray(int[] nums) {

        int even = 0;
        int odd = nums.length - 1;
        final int[] response = new int[nums.length];

        for(int i=0;i&amp;lt;nums.length;i++){
            if(isEven(nums[i])) {
                response[even] = 0;
                even++;
            } else {
                response[odd] = 1;
                odd--;
            }
        }

        return response;
    }

    public boolean isEven(final int num) {
        return num % 2 == 0; // &amp;amp; can be used as well
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 45.03 MB, less than 39.26% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is much faster than the previous one, not as clean, but better in performance as you can see on the runtime numbers if you compare with previous solution.&lt;/p&gt;




&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>array</category>
      <category>leetcode</category>
      <category>numbers</category>
    </item>
    <item>
      <title>LeetCode — 2161. Partition Array According to Given Pivot</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Mon, 26 May 2025 14:37:00 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2161-partition-array-according-to-given-pivot-mfm</link>
      <guid>https://dev.to/bendlmp/leetcode-2161-partition-array-according-to-given-pivot-mfm</guid>
      <description>&lt;p&gt;This is a medium problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:&lt;/p&gt;

&lt;p&gt;Every element less than pivot appears before every element greater than pivot.&lt;/p&gt;

&lt;p&gt;Every element equal to pivot appears in between the elements less than and greater than pivot.&lt;/p&gt;

&lt;p&gt;The relative order of the elements less than pivot and the elements greater than pivot is maintained.&lt;/p&gt;

&lt;p&gt;More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i &amp;lt; j and both elements are smaller (or larger) than pivot, then pi &amp;lt; pj.&lt;/p&gt;

&lt;p&gt;Return nums after the rearrangement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, so for the first and most simple thing that you would imagine, would be to separate in different arrays the small ones, biggest ones and fill up the rest. With that in mind the code would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] pivotArray(int[] nums, int pivot) {

        final List&amp;lt;Integer&amp;gt; beforePivot = new ArrayList&amp;lt;&amp;gt;();
        final List&amp;lt;Integer&amp;gt; afterPivot = new ArrayList&amp;lt;&amp;gt;();
        int amountThatsEqual = 0;

        for(int i=0 ; i &amp;lt; nums.length ; i++) {
            if(nums[i] &amp;lt; pivot) {
                beforePivot.add(nums[i]);
            } else if(nums[i] &amp;gt; pivot) {
                afterPivot.add(nums[i]);
            } else {
                amountThatsEqual++;
            }
        }

        final List&amp;lt;Integer&amp;gt; response = new ArrayList&amp;lt;&amp;gt;();

        response.addAll(beforePivot);

        IntStream.range(0, amountThatsEqual).forEach(i -&amp;gt; response.add(pivot));

        response.addAll(afterPivot);

        return response.stream().mapToInt(Integer::intValue).toArray();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 25ms, faster than 5.30% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 66.20 MB, less than 63.43% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It feels like a good memory usage, but the runtime is really bad. In this first try we use a lot of things that are not needed, and we could go into usage of arrays only. Let’s try to go for just one array and see how it goes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] pivotArray(int[] nums, int pivot) {
        final int[] response = new int[nums.length];
        int left = 0;

        for(int i=0 ; i&amp;lt;response.length ; i++) {
            if(nums[i] &amp;lt; pivot) {
                response[left] = nums[i];
                left++;
            }
        }

        for(int i=0 ; i&amp;lt;response.length ; i++) {
        if(nums[i] == pivot) {
                response[left] = pivot;
                left++;
            }
        }

        for(int i=0 ; i&amp;lt;response.length ; i++) {
            if(nums[i] &amp;gt; pivot) {
                response[left] = nums[i];
                left++;
            } 
        }

        return response;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 5ms, faster than 75.98% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 68.17 MB, less than 33.65% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not bad, runtime much faster, five times faster and we are using only one array, but the bad things is that we are making an iteration three times, and that could be reduced to only one if we used a thing called two pointers technique, which consists into having two markers, usually one at the beginning and the other at the end.&lt;/p&gt;

&lt;p&gt;With those you can make your checks and change as needed regarding the question in place.&lt;/p&gt;

&lt;p&gt;With that in mind let’s try out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] pivotArray(int[] nums, int pivot) {
        final int[] response = new int[nums.length];
        int left = 0;
        int right = nums.length - 1;

        // two pointes one from begining and the other from the end
        for(int i=0,j = nums.length-1 ; i&amp;lt;response.length ; i++, j--) {
            if(nums[i] &amp;lt; pivot) {
                response[left] = nums[i];
                left++;
            }
            if(nums[j] &amp;gt; pivot) {
                response[right] = nums[j];
                right--;
            } 
        }

        // fill the zeros
        while (left &amp;lt;= right) {
            response[left] = pivot;
            left++;
        }

        return response;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 5ms, faster than 75.98% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 67.28 MB, less than 52.84% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Very good, with that we keep good runtime speed, decrease a bit of memory usage and have it more clear and simple.&lt;/p&gt;




&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>arrays</category>
      <category>twopointers</category>
    </item>
    <item>
      <title>Singleton</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Sun, 13 Apr 2025 22:03:10 +0000</pubDate>
      <link>https://dev.to/bendlmp/singleton-3mj5</link>
      <guid>https://dev.to/bendlmp/singleton-3mj5</guid>
      <description>&lt;p&gt;At first glance, on the software world, singleton its’ a design pattern that means one unique instance only.&lt;/p&gt;

&lt;p&gt;By refactoring guru the word intent is to be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The word singleton comes from math, which from wikipedia states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A singleton (also known as a unit set[1] or one-point set) is a set with exactly one element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Singleton word, it’s a mix of two smaller ones, single and ton. Single word is pretty explanatory and -ton is used in surnames that come from place names, such as Newton, Hilton, Dayton, and Clinton.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But why would you use a singleton in your project?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First advantage of using it is the fact that you can have it as a place for your global variables or any type of global access. Instead of having to re-instantiate or look for properties or any other types of data source every time you want to reach for it (degrading I/O performance), if you load into a singleton object instance, that object will always be available for the whole application, as a single source of truth.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second advantage is that you don’t need / can’t have multiple objects for the same end if just one is enough for it, having more than one you just cause you to increase memory for no big reason and breaks the idea of singleton (unique instance).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What problems comes with singleton?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It makes harder to test your code since uses static (but this can be solved with dependency injection or usage of power mock).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It brakes single responsibility principles (solves 2 items — the advantages).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since it’s easy to share data/functionalities global, users tend to abuse it breaking it’s purpose (e.g. having multiple functionalities that are not made for the same end), so the team needs to be aware of it and use with caution.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One extra tip is to be aware of multi thread usage/access and how this can affect your singleton and related arguments if you make changes on attributes that are shared between the threads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can you create a singleton in Java?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You want a unique reference of your object, to do that you use static keyword.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You don’t want to have anyone to instance it, so you add a private constructor and make the instance in the class itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One last thing to be aware is that you can have the instance created in a static way (as soon as the application get’s started) or once someone requests a instance of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With those two ideas in place the result in a lazy instance is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Lazy instance
public class SingletonExample {

    public static SingletonExample instance;

    private SingletonExample() {
      // private constructor
    }

    public SingletonExample getInstance() {
        if (instance == null) {
            instance = new SingletonExample();
        }
        return instance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want an eager approach would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Eager instance
public class SingletonExample {

    public static SingletonExample instance = new SingletonExample();

    private SingletonExample() {
      // private constructor
    }

    public SingletonExample getInstance() {
        return instance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Today singleton is the most used design pattern in the software industry I would say, it’s well adopted and used everywhere.&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>singleton</category>
      <category>programming</category>
    </item>
    <item>
      <title>LeetCode — 2807. Insert Greatest Common Divisors in Linked List</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Mon, 24 Feb 2025 13:46:14 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2807-insert-greatest-common-divisors-in-linked-list-204l</link>
      <guid>https://dev.to/bendlmp/leetcode-2807-insert-greatest-common-divisors-in-linked-list-204l</guid>
      <description>&lt;p&gt;It’s a medium problem with the description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given the head of a linked list head, in which each node contains an integer value.&lt;/p&gt;

&lt;p&gt;Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.&lt;/p&gt;

&lt;p&gt;Return the linked list after insertion.&lt;/p&gt;

&lt;p&gt;The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: head = [18,6,10,3]&lt;br&gt;
Output: [18,6,6,2,10,1,3]&lt;/p&gt;

&lt;p&gt;Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes.&lt;/li&gt;
&lt;li&gt;We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes.&lt;/li&gt;
&lt;li&gt;We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are no more adjacent nodes, so we return the linked list.&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;The number of nodes in the list is in the range [1, 5000].&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= Node.val &amp;lt;= 1000&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For this problem first thing to understand is that you need to know how to get the greatest common divisor.&lt;/p&gt;

&lt;p&gt;An easy way to do would be use what’s already written through BigInteger gcd method:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;public BigInteger gcd(BigInteger val)&lt;/p&gt;

&lt;p&gt;Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). Returns 0 if this == 0 &amp;amp;&amp;amp; val == 0.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With that in hand, as the problem states you need to add new node in between the two nodes being calculated.&lt;/p&gt;

&lt;p&gt;Also since we don’t know how many nodes we have, we will have to loop indefinitely and once there is no next node we assume the loop is over.&lt;/p&gt;

&lt;p&gt;Wrapping up those ideas in mind, the solution proposed would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode insertGreatestCommonDivisors(ListNode head) {
        ListNode pivot = head;
        while(true) {
            if(Objects.isNull(pivot.next)){
                return head;
            }
            ListNode next = pivot.next;
            int gcd = getGreatestCommonDivisor(pivot.val, next.val);
            ListNode newNode = new ListNode(gcd, next);
            pivot.next = newNode;
            pivot = next;
        }
    }

    // Greatest Common Divisor from BigInteger class
    private int getGreatestCommonDivisor(int a, int b) {
        return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 16ms, faster than 19.39% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 45.45 MB, less than 19.34% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s a good solution, but still takes a lot of time to get it sorted out.&lt;/p&gt;

&lt;p&gt;In this case we could remove some instances references and also use a more primitive way of calculating the greatest common divisor, and with that achieve a 1ms runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode insertGreatestCommonDivisors(ListNode head) {
        ListNode pivot = head;
        while(true) {
            if(pivot.next==null){
                return head;
            }
            ListNode next = pivot.next;
            pivot.next = new ListNode(getGreatestCommonDivisor(pivot.val, next.val), next);
            pivot = next;
        }
    }

    // Greatest Common Divisor from BigInteger class
    private int getGreatestCommonDivisor(int a, int b) {
         while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 45.16 MB, less than 54.56% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>greatestcommondivisor</category>
      <category>listnode</category>
    </item>
    <item>
      <title>Leetcode — 1486. XOR Operation in an Array</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Fri, 31 Jan 2025 13:22:44 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-1486-xor-operation-in-an-array-3bj9</link>
      <guid>https://dev.to/bendlmp/leetcode-1486-xor-operation-in-an-array-3bj9</guid>
      <description>&lt;p&gt;It’s an easy problem with description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given an integer n and an integer start.&lt;/p&gt;

&lt;p&gt;Define an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length.&lt;/p&gt;

&lt;p&gt;Return the bitwise XOR of all elements of nums.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: n = 5, start = 0&lt;br&gt;
Output: 8&lt;br&gt;
Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.&lt;br&gt;
Where "^" corresponds to bitwise XOR operator.&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;Input: n = 4, start = 3&lt;br&gt;
Output: 8&lt;br&gt;
Explanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= n &amp;lt;= 1000&lt;/p&gt;

&lt;p&gt;0 &amp;lt;= start &amp;lt;= 1000&lt;/p&gt;

&lt;p&gt;n == nums.length&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, to get this sorted out, you need to understand what XOR operator means.&lt;/p&gt;

&lt;p&gt;This operator means exclusivity, meaning if both have same values, they will not get counted, if not then they will.&lt;/p&gt;

&lt;p&gt;In this case with numbers it goes bitwise (binary numeral), so you update numbers from let’s say 9 to 1001 and use the binaries, on each, to determine if they are exclusive from one number to the other.&lt;/p&gt;

&lt;p&gt;One example would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;9 being 1001&lt;/li&gt;
&lt;li&gt;8 being 1000&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It shows just one exclusive number there, the first one (1 for 9 and 0 for 8), and due to that 8 ^ 9 = 1&lt;/p&gt;

&lt;p&gt;So getting back into your problem, for us to return the bitwise XOR of all elements of nums you basically have to iterate based on n and use XOR on a sum, for each iteration, with the code being:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int xorOperation(int n, int start) {
        int bitwiseXor = 0;
        for(int i=0 ; i &amp;lt; n ; i++) {
            bitwiseXor = bitwiseXor ^ (start + 2 * i);
        }
        return bitwiseXor;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 40.04 MB, less than 95.84% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>xor</category>
      <category>exclusive</category>
    </item>
    <item>
      <title>Leetcode — 3146. Permutation Difference between Two Strings</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Fri, 17 Jan 2025 13:56:55 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-3146-permutation-difference-between-two-strings-3b28</link>
      <guid>https://dev.to/bendlmp/leetcode-3146-permutation-difference-between-two-strings-3b28</guid>
      <description>&lt;p&gt;It’s an easy problem with description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given two strings s and t such that every character occurs at most once in s and t is a permutation of s.&lt;/p&gt;

&lt;p&gt;The permutation difference between s and t is defined as the sum of the absolute difference between the index of the occurrence of each character in s and the index of the occurrence of the same character in t.&lt;/p&gt;

&lt;p&gt;Return the permutation difference between s and t.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: s = “abc”, t = “bac”&lt;/p&gt;

&lt;p&gt;Output: 2&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;For s = "abc" and t = "bac", the permutation difference of s and t is equal to the sum of:&lt;/p&gt;

&lt;p&gt;The absolute difference between the index of the occurrence of "a" in s and the index of the occurrence of "a" in t.&lt;/p&gt;

&lt;p&gt;The absolute difference between the index of the occurrence of "b" in s and the index of the occurrence of "b" in t.&lt;/p&gt;

&lt;p&gt;The absolute difference between the index of the occurrence of "c" in s and the index of the occurrence of "c" in t.&lt;/p&gt;

&lt;p&gt;That is, the permutation difference between s and t is equal to |0 - 1| + |1 - 0| + |2 - 2| = 2.&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;Input: s = “abcde”, t = “edbac”&lt;/p&gt;

&lt;p&gt;Output: 12&lt;/p&gt;

&lt;p&gt;Explanation: The permutation difference between s and t is equal to |0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12.&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= s.length &amp;lt;= 26&lt;/p&gt;

&lt;p&gt;Each character occurs at most once in s.&lt;/p&gt;

&lt;p&gt;t is a permutation of s.&lt;/p&gt;

&lt;p&gt;s consists only of lowercase English letters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is a lot of pretty words to describe what needs to be done, but once you see one example it makes it easier to understand what is that the problem is trying to accomplish.&lt;/p&gt;

&lt;p&gt;Following the permutation difference calculation there is an understatement that needs to be done is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterate the String;&lt;/li&gt;
&lt;li&gt;Retrieve the character based on the index;&lt;/li&gt;
&lt;li&gt;Find this character index on the second String;&lt;/li&gt;
&lt;li&gt;Subtract first index with second one;&lt;/li&gt;
&lt;li&gt;Get the absolute value from that subtraction and collect all into one output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s translate that thought into java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int findPermutationDifference(String s, String t) {

        int output = 0;

        for(int i=0; i &amp;lt;s.length(); i++) {
            final char c = s.charAt(i);
            final int j = t.indexOf(c);
            output += Math.abs(i - j);
        }

        return output;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 100% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 42.67 MB, less than 57.64% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s a good solution that’s also very good performance wise. If we want to be more elegant and follow the solution with a Stream the solution would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int findPermutationDifference(String s, String t) {
        return IntStream.range(0, s.length())
                   .map(i -&amp;gt; findCharPermutationDifference(s,t,i))
                   .sum();
    }

    public int findCharPermutationDifference(final String s, final String t, final int i) {
        final char c = s.charAt(i);
        final int j = t.indexOf(c);
        return Math.abs(i - j);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 5ms, faster than 2.31% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 43.02 MB, less than 23.05% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not as good performance and memory wise, but ellegant nonetheless.&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>string</category>
      <category>permutations</category>
    </item>
    <item>
      <title>Leetcode — 2942. Find Words Containing Character</title>
      <dc:creator>Ben Pereira</dc:creator>
      <pubDate>Tue, 07 Jan 2025 14:32:14 +0000</pubDate>
      <link>https://dev.to/bendlmp/leetcode-2942-find-words-containing-character-1pji</link>
      <guid>https://dev.to/bendlmp/leetcode-2942-find-words-containing-character-1pji</guid>
      <description>&lt;p&gt;It’s an easy problem with description being:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given a 0-indexed array of strings words and a character x.&lt;/p&gt;

&lt;p&gt;Return an array of indices representing the words that contain the character x.&lt;/p&gt;

&lt;p&gt;Note that the returned array may be in any order.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: words = ["leet","code"], x = "e"&lt;br&gt;
Output: [0,1]&lt;br&gt;
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;Input: words = ["abc","bcd","aaaa","cbc"], x = "a"&lt;br&gt;
Output: [0,2]&lt;br&gt;
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.&lt;/p&gt;

&lt;p&gt;Example 3:&lt;/p&gt;

&lt;p&gt;Input: words = ["abc","bcd","aaaa","cbc"], x = "z"&lt;br&gt;
Output: []&lt;br&gt;
Explanation: "z" does not occur in any of the words. Hence, we return an empty array.&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= words.length &amp;lt;= 50&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= words[i].length &amp;lt;= 50&lt;/p&gt;

&lt;p&gt;x is a lowercase English letter.&lt;/p&gt;

&lt;p&gt;words[i] consists only of lowercase English letters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To solve this problem you will need to iterate the words list, on each word check if contains the char, if so you store it’s index into a response list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public List&amp;lt;Integer&amp;gt; findWordsContaining(String[] words, char x) {
        // create response
        final List&amp;lt;Integer&amp;gt; indexes = new ArrayList&amp;lt;&amp;gt;();

        // iterate words string array
        for(int i=0;i&amp;lt;words.length;i++){
            // check if char exists into the word
            if(words[i].indexOf(x) != -1){
                indexes.add(i); // if yes add index into the response
            }
        }

        // return searched indexes
        return indexes;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 1ms, faster than 100.00% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 44.95 MB, less than 49.76% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;If you want to go into a lambda/function approach, which usually is cleaner but takes more toll on performance, it would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public List&amp;lt;Integer&amp;gt; findWordsContaining(String[] words, char x) {
        return IntStream.range(0, words.length)
                .boxed() // convert primitive into Class related (int -&amp;gt; Integer)
                .map(i -&amp;gt; getIndexIfCharExistsInWord(words[i], i, x))
                .filter(Objects::nonNull) // to remove null ones from mapping
                .collect(Collectors.toList());
    }

    public Integer getIndexIfCharExistsInWord(final String word, final int i, final char x) {
        return word.indexOf(x) != -1 ? i : null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Runtime: 9ms, faster than 2.72% of Java online submissions.&lt;/p&gt;

&lt;p&gt;Memory Usage: 44.90 MB, less than 66.32% of Java online submissions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.&lt;/p&gt;

&lt;p&gt;Until next post! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>arrays</category>
      <category>string</category>
    </item>
  </channel>
</rss>
