<?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: Simona Cancian</title>
    <description>The latest articles on DEV Community by Simona Cancian (@simona-cancian).</description>
    <link>https://dev.to/simona-cancian</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%2F1714974%2Ff97d395b-0eac-4567-bf1d-b1ac4b9e5658.jpg</url>
      <title>DEV Community: Simona Cancian</title>
      <link>https://dev.to/simona-cancian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simona-cancian"/>
    <language>en</language>
    <item>
      <title>Leetcode Day 9: Find the Index of the First Occurrence in a String Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Thu, 11 Jul 2024 05:44:26 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-9-find-the-index-of-the-first-occurrence-in-a-string-explained-4kg2</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-9-find-the-index-of-the-first-occurrence-in-a-string-explained-4kg2</guid>
      <description>&lt;p&gt;&lt;strong&gt;The problem is as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given two strings &lt;code&gt;needle&lt;/code&gt; and &lt;code&gt;haystack&lt;/code&gt;, return the index of the first occurrence of &lt;code&gt;needle&lt;/code&gt; in &lt;code&gt;haystack&lt;/code&gt;, or &lt;code&gt;-1&lt;/code&gt; if &lt;code&gt;needle&lt;/code&gt; is not part of &lt;code&gt;haystack&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This is how I solved it:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the first easy problem that was actually easy. Just use the built-in &lt;code&gt;index()&lt;/code&gt; function, and that's it!&lt;br&gt;
This is how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if 'needle' is a substring of 'haystack'&lt;/li&gt;
&lt;li&gt;If it is, return the index of the first occurrence of 'needle'&lt;/li&gt;
&lt;li&gt;Else if 'needle' is not found, return -1
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if needle in haystack:
    return haystack.index(needle)
else:
    return -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;This is the completed solution:&lt;/strong&gt;&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:
    def strStr(self, haystack: str, needle: str) -&amp;gt; int:
        return haystack.index(needle) if needle in haystack else -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>python</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 8: Remove Element Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Tue, 09 Jul 2024 02:10:40 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-8-remove-element-explained-212a</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-8-remove-element-explained-212a</guid>
      <description>&lt;p&gt;&lt;strong&gt;The problem is as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;val&lt;/code&gt;, remove all occurrences of &lt;code&gt;val&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt; &lt;em&gt;in-place&lt;/em&gt;. The order of the elements may be changed. Then return the &lt;em&gt;number of elements in &lt;code&gt;nums&lt;/code&gt; which are not equal to &lt;code&gt;val&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Consider the number of elements in &lt;code&gt;nums&lt;/code&gt; which are not equal to &lt;code&gt;val&lt;/code&gt; be &lt;code&gt;k&lt;/code&gt;, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the array &lt;code&gt;nums&lt;/code&gt; such that the first &lt;code&gt;k&lt;/code&gt; elements of &lt;code&gt;nums&lt;/code&gt; contain the elements which are not equal to &lt;code&gt;val&lt;/code&gt;. The remaining elements of &lt;code&gt;nums&lt;/code&gt; are not important as well as the size of &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return k.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Custom Judge:&lt;/p&gt;

&lt;p&gt;The judge will test your solution with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
                            // It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i &amp;lt; actualLength; i++) {
    assert nums[i] == expectedNums[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all assertions pass, then your solution will be accepted.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here is how I solved it:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve this problem, I used two main strategies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;In-place Replacement&lt;/em&gt;: Instead of creating a new array to store the elements that are not equal to &lt;code&gt;val&lt;/code&gt;, use the same array &lt;code&gt;nums&lt;/code&gt; to overwrite the elements that need to be removed.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Two-pointer Technique&lt;/em&gt;: One pointer (&lt;code&gt;i&lt;/code&gt;) iterates through each element in the array, and another pointer (&lt;code&gt;k&lt;/code&gt;) keeps track of the position where the next non-val element should be placed.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;First, initialize a pointer &lt;code&gt;k&lt;/code&gt; and set it to 0. This will keep track of the position where the next non-val element should be placed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
def removeElement(self, nums: List[int], val: int) -&amp;gt; int:       
    k = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Iterate through nums array.&lt;/li&gt;
&lt;li&gt;Check if the current element nums[i] is different from val to keep track of k.&lt;/li&gt;
&lt;li&gt;If it is, move element nums[i] to the k-th position, and increment k by 1 to update the position for the next non-val element.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(nums)):
    if nums[i] != val:
        nums[k] = nums[i]
        k += 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Return k, which is the number of elements not equal to val.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return k
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here is the completed solution:&lt;/strong&gt;&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:
    def removeElement(self, nums: List[int], val: int) -&amp;gt; int:
        k = 0
        for i in range(len(nums)):
            if nums[i] != val:
                nums[k] = nums[i]
                k += 1
        return k
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 7: Remove Duplicated from Sorted Array Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Mon, 08 Jul 2024 03:25:02 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-7-remove-duplicated-from-sorted-array-explained-3oe</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-7-remove-duplicated-from-sorted-array-explained-3oe</guid>
      <description>&lt;p&gt;&lt;strong&gt;The problem is as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return t_he number of unique elements in &lt;code&gt;nums&lt;/code&gt;_.&lt;/p&gt;

&lt;p&gt;Consider the number of unique elements of &lt;code&gt;nums&lt;/code&gt; to be &lt;code&gt;k&lt;/code&gt;, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the array &lt;code&gt;nums&lt;/code&gt; such that the first &lt;code&gt;k&lt;/code&gt; elements of &lt;code&gt;nums&lt;/code&gt; contain the unique elements in the order they were present in &lt;code&gt;nums&lt;/code&gt; initially. The remaining elements of &lt;code&gt;nums&lt;/code&gt; are not important as well as the size of &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Custom Judge:&lt;/p&gt;

&lt;p&gt;The judge will test your solution with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i &amp;lt; k; i++) {
    assert nums[i] == expectedNums[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all assertions pass, then your solution will be accepted.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here is how I solved it:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, initialize a pointer &lt;code&gt;k&lt;/code&gt; and set it to 0. This pointer will keep track of the position of the last unique element in the array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def removeDuplicates(self, nums: List[int]) -&amp;gt; int:
        # Initialize a pointer 'k' and set it to 0
        k = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Loop through &lt;code&gt;nums&lt;/code&gt; array from the second element (index 1). The first element is always unique, so we can skip it for comparison purposes.&lt;/li&gt;
&lt;li&gt;Check for duplicates: if the current element &lt;code&gt;nums[i]&lt;/code&gt; is different from the last unique element &lt;code&gt;nums[k]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If it is, it means we have found a new unique element. Move to the next element and update &lt;code&gt;nums[k]&lt;/code&gt; to current element &lt;code&gt;nums[i]&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, len(nums)):
    if nums and nums[i] != nums[k]:
        k += 1
        nums[k] = nums[i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After the loop, &lt;code&gt;k&lt;/code&gt; will be the index of the last unique element, so the total number of unique elements is &lt;code&gt;k + 1&lt;/code&gt;. Return &lt;code&gt;k&lt;/code&gt; + 1 because &lt;code&gt;k&lt;/code&gt; starts at 0'
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return k + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here is the completed solution:&lt;/strong&gt;&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:
    def removeDuplicates(self, nums: List[int]) -&amp;gt; int:
        k = 0
        for i in range(1, len(nums)):
            if nums and nums[i] != nums[k]:
                k += 1
                nums[k] = nums[i]
        return k + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>python</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 6: Merge Two Sorted Lists Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Sun, 07 Jul 2024 00:35:13 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-6-merge-two-sorted-lists-explained-55n2</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-6-merge-two-sorted-lists-explained-55n2</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
You are given the heads of two sorted linked lists &lt;code&gt;list1&lt;/code&gt; and &lt;code&gt;list2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.&lt;/p&gt;

&lt;p&gt;Return the head of the merged linked list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F4hxbclvs3gyb0zfbtal7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4hxbclvs3gyb0zfbtal7.jpg" alt="Image representation of how two merged sorted linked lists looks"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Input: list1 = [], list2 = []
Output: []


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Input: list1 = [], list2 = [0]
Output: [0]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here is how I solved it:&lt;br&gt;
Let's go through the provided definition for a singly-linked list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class ListNode:
    def __init__(self, val=0, next=None):
        # Data stored in node val
        self.val = val
        # Reference to the next node
        self.next = next


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A singly linked list is a linear data structure where elements are connected in a sequence, and each element points to the next one using a pointer.&lt;/p&gt;

&lt;p&gt;With that said, let's step through the logic here. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize a dummy_node that represents the starting point for the new merges sorted list.&lt;/li&gt;
&lt;li&gt;Set it to a pointer current_node (address in RAM, aka a variable) to construct the new list. Initially, this pointer will point to the dummy node.
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;class Solution:&lt;br&gt;
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -&amp;gt; Optional[ListNode]:&lt;br&gt;
        dummy_node = ListNode()&lt;br&gt;
        current_node = dummy_node&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Use a while loop to iterate through both lists at the same time until of them is null.
- Choose the head with the smaller value of the two lists: if value of current node in `list1` is less than value of node in `list2`, then append the `list1` node to the merged list and move to next node in `list1`.
- Else, append the `list2` node to merged list and move to next node in `list2`.
- We are still in the while loop. Move the current_node pointer to the newly added node

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while list1 and list2:&lt;br&gt;
    if list1.val &amp;lt; list2.val:&lt;br&gt;&lt;br&gt;
        current_node.next = list1&lt;br&gt;
        list1 = list1.next&lt;br&gt;
    else:&lt;br&gt;
        current_node.next = list2&lt;br&gt;
        list2 = list2.next&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current_node = current_node.next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- After the while loop, if there are remaining nodes in `list1` or `list2`, append them to the merged list.
- Return the head of the merged list, which is the next node of the dummy node.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if list1:&lt;br&gt;
    current_node.next = list1&lt;br&gt;
else:&lt;br&gt;
    current_node.next = list2&lt;/p&gt;

&lt;p&gt;return dummy_node.next&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Here is the completed solution:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;class Solution:&lt;br&gt;
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -&amp;gt; Optional[ListNode]:&lt;br&gt;
        dummy_node = ListNode()&lt;br&gt;
        current_node = dummy_node&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while list1 and list2:
        if list1.val &amp;lt; list2.val:
            current_node.next = list1
            list1 = list1.next
        else:
            current_node.next = list2
            list2 = list2.next

        current_node = current_node.next

    if list1:
        current_node.next = list1
    else:
        current_node.next = list2

    return dummy_node.next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 5: Valid Parentheses Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Sat, 06 Jul 2024 07:39:28 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-5-valid-parentheses-explained-18jf</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-5-valid-parentheses-explained-18jf</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
Given a string s containing just the characters &lt;code&gt;'('&lt;/code&gt;, &lt;code&gt;')'&lt;/code&gt;, &lt;code&gt;'{'&lt;/code&gt;, &lt;code&gt;'}'&lt;/code&gt;, &lt;code&gt;'['&lt;/code&gt; and &lt;code&gt;']'&lt;/code&gt;, determine if the input string is valid.&lt;/p&gt;

&lt;p&gt;An input string is valid if:&lt;/p&gt;

&lt;p&gt;Open brackets must be closed by the same type of brackets.&lt;br&gt;
Open brackets must be closed in the correct order.&lt;br&gt;
Every close bracket has a corresponding open bracket of the same type.&lt;/p&gt;

&lt;p&gt;Here is how I solved it:&lt;br&gt;
This is a good example on when to use a stack data structure. We are going to use a stack to keep track of the open brackets and ensure they are closed correctly and in the right order. Let's first figure out what is a stack. Look at this image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzurkuh0u4os10wsg4zi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzurkuh0u4os10wsg4zi.png" alt="Demonstration of a stack data structure" width="260" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A stack is a Last-In-First-Out (LIFO) data structure, meaning the last element added (pushed) to the stack is the first one to be removed (popped). This makes it ideal for matching parentheses, as we need to ensure that the most recent open bracket is closed by the corresponding close bracket.&lt;/p&gt;

&lt;p&gt;Let's step through the logic.&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:
    def isValid(self, s: str) -&amp;gt; bool:
        // my solution here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create an empty stack to keep track of open brackets.&lt;/li&gt;
&lt;li&gt;Create a dictionary to map each type of open bracket to its corresponding close bracket.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack = []
mapping_parentheses = {"(" : ")", "[" : "]", "{" : "}"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; Loop through each character in the string &lt;code&gt;s&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the character is an open bracket (i.e., it is a key in the mapping_parentheses dictionary), "push" it onto the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Else if the character is a close bracket, check two conditions:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;not stack&lt;/code&gt;: If the stack is empty, it means there is no corresponding open bracket for the close bracket, so return False.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;char != mapping_parentheses[stack.pop()]&lt;/code&gt;: Pop the top element from the stack and check if the popped element matches the current close bracket using the dictionary. If not, return False.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for char in s: 
    if char in mapping_parentheses: 
        stack.append(char)
    elif not stack or char != mapping_parentheses[stack.pop()]:
        return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After processing all characters, if the stack is empty, it means all open brackets were correctly matched and closed. If the stack is not empty, it means there are unmatched open brackets, so return False.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return not stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the completed solution:&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:
    def isValid(self, s: str) -&amp;gt; bool:
        stack = []
        mapping_parentheses = {"(" : ")", "[" : "]", "{" : "}"}

        for char in s: 
            if char in mapping_parentheses: 
                stack.append(char)
            elif not stack or char != mapping_parentheses[stack.pop()]:
                return False
        return not stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 5: Valid Parentheses Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Sat, 06 Jul 2024 07:39:27 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-5-valid-parentheses-explained-5d97</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-5-valid-parentheses-explained-5d97</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
Given a string s containing just the characters &lt;code&gt;'('&lt;/code&gt;, &lt;code&gt;')'&lt;/code&gt;, &lt;code&gt;'{'&lt;/code&gt;, &lt;code&gt;'}'&lt;/code&gt;, &lt;code&gt;'['&lt;/code&gt; and &lt;code&gt;']'&lt;/code&gt;, determine if the input string is valid.&lt;/p&gt;

&lt;p&gt;An input string is valid if:&lt;/p&gt;

&lt;p&gt;Open brackets must be closed by the same type of brackets.&lt;br&gt;
Open brackets must be closed in the correct order.&lt;br&gt;
Every close bracket has a corresponding open bracket of the same type.&lt;/p&gt;

&lt;p&gt;Here is how I solved it:&lt;br&gt;
This is a good example on when to use a stack data structure. We are going to use a stack to keep track of the open brackets and ensure they are closed correctly and in the right order. Let's first figure out what is a stack. Look at this image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzurkuh0u4os10wsg4zi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzurkuh0u4os10wsg4zi.png" alt="Demonstration of a stack data structure" width="260" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A stack is a Last-In-First-Out (LIFO) data structure, meaning the last element added (pushed) to the stack is the first one to be removed (popped). This makes it ideal for matching parentheses, as we need to ensure that the most recent open bracket is closed by the corresponding close bracket.&lt;/p&gt;

&lt;p&gt;Let's step through the logic.&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:
    def isValid(self, s: str) -&amp;gt; bool:
        // my solution here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create an empty stack to keep track of open brackets.&lt;/li&gt;
&lt;li&gt;Create a dictionary to map each type of open bracket to its corresponding close bracket.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack = []
mapping_parentheses = {"(" : ")", "[" : "]", "{" : "}"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; Loop through each character in the string &lt;code&gt;s&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the character is an open bracket (i.e., it is a key in the mapping_parentheses dictionary), "push" it onto the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Else if the character is a close bracket, check two conditions:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;not stack&lt;/code&gt;: If the stack is empty, it means there is no corresponding open bracket for the close bracket, so return False.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;char != mapping_parentheses[stack.pop()]&lt;/code&gt;: Pop the top element from the stack and check if the popped element matches the current close bracket using the dictionary. If not, return False.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for char in s: 
    if char in mapping_parentheses: 
        stack.append(char)
    elif not stack or char != mapping_parentheses[stack.pop()]:
        return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After processing all characters, if the stack is empty, it means all open brackets were correctly matched and closed. If the stack is not empty, it means there are unmatched open brackets, so return False.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return not stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the completed solution:&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:
    def isValid(self, s: str) -&amp;gt; bool:
        stack = []
        mapping_parentheses = {"(" : ")", "[" : "]", "{" : "}"}

        for char in s: 
            if char in mapping_parentheses: 
                stack.append(char)
            elif not stack or char != mapping_parentheses[stack.pop()]:
                return False
        return not stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 4: Longest Common Prefix Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Fri, 05 Jul 2024 05:08:18 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-4-longest-common-prefix-explained-62n</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-4-longest-common-prefix-explained-62n</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
Write a function to find the longest common prefix string amongst an array of strings.&lt;/p&gt;

&lt;p&gt;If there is no common prefix, return an empty string &lt;code&gt;""&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is how I solved it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there input list of strings is empty, it means that there is no common prefix to check so we return an empty string.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if not strs:
    return ""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Use the first string in the list as a reference for comparison
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(strs[0])):
    char = strs[0][i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;For each character in the reference string, compare it with the corresponding character in all other strings. If any string has a different character at the same position or is shorter than current position &lt;code&gt;i&lt;/code&gt;, it means that the common prefix ends before this character. Therefore, return the substring of the reference string up to this point.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for string in strs[1:]:
    if i &amp;gt;= len(string) or string[i] != char:
        return strs[0][:i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;If loops completes without returning, it means that the reference string itself is the longest common prefix
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    return strs[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;OR just use &lt;code&gt;os&lt;/code&gt; standard library available in Python. It includes functions for file and directory manipulation, environment variables, process management etc.&lt;br&gt;
The &lt;code&gt;os&lt;/code&gt; module provides a way to interact with the operating system in a portable way.&lt;br&gt;
Here is the completed solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -&amp;gt; str:
        prefix = os.path.commonprefix(strs)
        return prefix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 3: Roman to Integer Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Thu, 04 Jul 2024 04:38:26 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-3-roman-to-integer-explained-329o</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-3-roman-to-integer-explained-329o</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
Roman numerals are represented by seven different symbols: &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;V&lt;/code&gt;, &lt;code&gt;X&lt;/code&gt;, &lt;code&gt;L&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt; and &lt;code&gt;M&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Symbol: Value&lt;br&gt;
I: 1&lt;br&gt;
V: 5&lt;br&gt;
X: 10&lt;br&gt;
L: 50&lt;br&gt;
C: 100&lt;br&gt;
D: 500&lt;br&gt;
M: 1000&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;2&lt;/code&gt; is written as &lt;code&gt;II&lt;/code&gt; in Roman numeral, just two ones added together. &lt;code&gt;12&lt;/code&gt; is written as &lt;code&gt;XII&lt;/code&gt;, which is simply &lt;code&gt;X + II&lt;/code&gt;. The number &lt;code&gt;27&lt;/code&gt; is written as &lt;code&gt;XXVII&lt;/code&gt;, which is &lt;code&gt;XX + V + II&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not &lt;code&gt;IIII&lt;/code&gt;. Instead, the number four is written as &lt;code&gt;IV&lt;/code&gt;. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as &lt;code&gt;IX&lt;/code&gt;. There are six instances where subtraction is used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;I&lt;/code&gt; can be placed before &lt;code&gt;V&lt;/code&gt; (5) and &lt;code&gt;X&lt;/code&gt; (10) to make 4 and 9. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;X&lt;/code&gt; can be placed before &lt;code&gt;L&lt;/code&gt; (50) and &lt;code&gt;C&lt;/code&gt; (100) to make 40 and 90. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;C&lt;/code&gt; can be placed before &lt;code&gt;D&lt;/code&gt; (500) and &lt;code&gt;M&lt;/code&gt; (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is how I solved it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's make use of a dictionary to map each roman numeral to its integer value.&lt;/li&gt;
&lt;li&gt;Initialize a variable result to 0, which we will use to store the final integer value.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;roman_numbers = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}      
result = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Iterate through the string.&lt;/li&gt;
&lt;li&gt;If current character is greater than previous character, subtract twice the value of "s[char - 1]" and add the value of "s[char]". Here is where we handle the subtraction cases. 
For example, &lt;code&gt;IV = 4&lt;/code&gt; (&lt;code&gt;I=1&lt;/code&gt; is less than &lt;code&gt;V=5&lt;/code&gt;, so add &lt;code&gt;5&lt;/code&gt; and subtract &lt;code&gt;1&lt;/code&gt; twice: &lt;code&gt;5 - 2 + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Else, add the value of "s[char]" to the result. Here is where we handle the regular addition.&lt;/li&gt;
&lt;li&gt;Return result, which is the integer value of the roman numeral.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for char in range(len(s)):
    if char &amp;gt; 0 and roman_numbers[s[char]] &amp;gt; roman_numbers[s[char - 1]]:             
        result += roman_numbers[s[char]] - 2 * roman_numbers[s[char - 1]]
    else:
        result += roman_numbers[s[char]]
return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here is the completed solution:&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:
    def romanToInt(self, s: str) -&amp;gt; int:
        roman_numbers = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}      
        result = 0

        for char in range(len(s)):            
            if char &amp;gt; 0 and roman_numbers[s[char]] &amp;gt; roman_numbers[s[char - 1]]:                
                result += roman_numbers[s[char]] - 2 * roman_numbers[s[char - 1]]
            else:               
                result += roman_numbers[s[char]]
        return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>leetcode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 2: Palindrome Number Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Wed, 03 Jul 2024 02:19:12 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-2-palindrome-number-377i</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-2-palindrome-number-377i</guid>
      <description>&lt;p&gt;The problem is as follows:&lt;br&gt;
Given an integer &lt;code&gt;x&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;x&lt;/code&gt; is a &lt;strong&gt;palindrome&lt;/strong&gt;, and &lt;code&gt;false&lt;/code&gt; &lt;em&gt;otherwise&lt;/em&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore, it is not a palindrome.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore, it is not a palindrome.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how I solved it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's convert the given integer into a string&lt;/li&gt;
&lt;li&gt;In Python we can reverse a string by slicing: create a slice that starts at the end of the string and moves backwards. The slice statement &lt;code&gt;[::-1]&lt;/code&gt; means start and end of the string and end at position 0, move with the step &lt;code&gt;-1&lt;/code&gt;, which means one step backwards. &lt;a href="https://www.w3schools.com/python/python_howto_reverse_string.asp"&gt;https://www.w3schools.com/python/python_howto_reverse_string.asp&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rev = str(x)[::-1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let's compare the reversed string to the given integer (again, convert it to string: we can only compare same datatypes). Return True if it matches, else False.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if rev == str(x):
    return True
return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But hey, we can actually write just one line of code using ternary operator! To be honest, I am not sure if it's bad design, but it seemed more readable to me.&lt;br&gt;
Here is the complete solution:&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:
    def isPalindrome(self, x: int) -&amp;gt; bool:
        return True if str(x)[::-1] == str(x) else False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>python</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Leetcode Day 1: Two Sum Explained</title>
      <dc:creator>Simona Cancian</dc:creator>
      <pubDate>Tue, 02 Jul 2024 05:34:12 +0000</pubDate>
      <link>https://dev.to/simona-cancian/leetcode-day-1-two-sum-45fp</link>
      <guid>https://dev.to/simona-cancian/leetcode-day-1-two-sum-45fp</guid>
      <description>&lt;p&gt;The problem is as follow:&lt;/p&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;target&lt;/code&gt;, &lt;em&gt;return indices of the two numbers such that they add up to &lt;code&gt;target&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have &lt;strong&gt;&lt;em&gt;exactly&lt;/em&gt; one solution&lt;/strong&gt;, and you may not use the &lt;em&gt;same&lt;/em&gt; element twice.&lt;/p&gt;

&lt;p&gt;You can return the answer in any order.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [2,7,11,15], target = 9
Output: [0,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [3,2,4], target = 6
Output: [1,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [3,3], target = 6
Output: [0,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how I solved it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We want to create a dictionary named &lt;code&gt;index_map&lt;/code&gt; to store the integers in &lt;code&gt;nums&lt;/code&gt; and their corresponding indices.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index_map = {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then, we will use enumerate to get both index &lt;code&gt;i&lt;/code&gt; and value &lt;code&gt;num&lt;/code&gt; of each element in &lt;code&gt;nums&lt;/code&gt;.
For each integer, let's calculate the complement, which is the difference between the &lt;code&gt;target&lt;/code&gt; and the current element &lt;code&gt;num&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i, num in enumerate(nums):
    n = target - num
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now check the dictionary: if n is in the dictionary, it means we have found the two integers that add up to the &lt;code&gt;target&lt;/code&gt; Return the &lt;code&gt;n&lt;/code&gt; index and the current index as a list.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if n in index_map: 
    return [index_map[n], i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Else, if &lt;code&gt;n&lt;/code&gt; is not in the dictionary, add the current element &lt;code&gt;num&lt;/code&gt; and index to the dictionary.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index_map[num] = i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the completed solution:&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:
    def twoSum(self, nums: List[int], target: int) -&amp;gt; List[int]:
        index_map = {}
        for i, num in enumerate(nums):
            n = target - num
            if n in index_map: 
                return [index_map[n], i]
            index_map[num] = i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>python</category>
      <category>coding</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
