<?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>Forem: Charles Amakoye</title>
    <description>The latest articles on Forem by Charles Amakoye (@charlesamakoye).</description>
    <link>https://forem.com/charlesamakoye</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%2F251592%2F8e758369-088a-4122-86ad-d3e0e6c27021.jpg</url>
      <title>Forem: Charles Amakoye</title>
      <link>https://forem.com/charlesamakoye</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/charlesamakoye"/>
    <language>en</language>
    <item>
      <title>Dissecting the two pointer technique</title>
      <dc:creator>Charles Amakoye</dc:creator>
      <pubDate>Sat, 10 Aug 2024 21:03:47 +0000</pubDate>
      <link>https://forem.com/charlesamakoye/dissecting-the-two-pointer-technique-2lb4</link>
      <guid>https://forem.com/charlesamakoye/dissecting-the-two-pointer-technique-2lb4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction.
&lt;/h2&gt;

&lt;p&gt;Even after mastering the basics of arrays and lists, solving problems related to these data structures can sometimes feel overwhelming. Beyond understanding the data structures themselves - along with their operations and time and space complexities; grasping various techniques that apply to these problems can make the process much easier.&lt;br&gt;
This is especially true given the wide variety of problems that can arise with arrays or lists. In this blog post, I'll focus on one such technique: the two-pointer technique, which is particularly effective for tackling array or list problems.&lt;/p&gt;
&lt;h2&gt;
  
  
  Two pointers.
&lt;/h2&gt;

&lt;p&gt;The two pointer technique is an algorithmic approach, particularly effective for solving arrays or sequences. This means that the approach can also be applied to strings and linked lists.&lt;br&gt;
It involves use of two distinct pointers to traverse the data structure, often leading to efficient solution with lower time complexity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Types of two pointers.
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Pointers moving towards each other.
&lt;/h3&gt;

&lt;p&gt;This approach involves two pointers starting from opposite ends of the data structure and moving toward each other, meaning the pointers move in opposite directions. This type of two-pointer technique is particularly useful in scenarios where you want to find a pair of elements that meet certain conditions or when comparing elements from both ends. Common use cases include checking for a palindrome or finding pairs with a specific sum&lt;/p&gt;
&lt;h4&gt;
  
  
  Approach
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Initialize the Pointers: Start with one pointer at the beginning (left) and the other at the end (right) of the data structure.&lt;/li&gt;
&lt;li&gt;Move the Pointers: Adjust the pointers towards each other based on the given conditions.&lt;/li&gt;
&lt;li&gt;Check Conditions: Continue moving the pointers toward each other until the desired result is found or a specific condition is met&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;strong&gt;Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.&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;def reverseWords(s: str) -&amp;gt; str:
    words = s.split()  # Split the input string into words

    for i in range(len(words)):
        left = 0  # Initialize the left pointer
        right = len(words[i]) - 1  # Initialize the right pointer
        split_word = list(words[i])  # Convert the word into a list of characters

        while left &amp;lt; right:  # Continue until the pointers meet in the middle
            # Swap the characters at the left and right pointers
            temp = split_word[left]
            split_word[left] = split_word[right]
            split_word[right] = temp

            left += 1  # Move the left pointer to the right
            right -= 1  # Move the right pointer to the left

        words[i] = "".join(split_word)  # Rejoin the characters into a word

    return " ".join(words)  # Join the reversed words into a final string

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pointers moving in the same direction.
&lt;/h3&gt;

&lt;p&gt;In this approach, both pointers start from the same end of the data structure and move in the same direction. This technique is often used when you need to track a window or sub-array within an array, allowing you to efficiently move and adjust the window based on certain conditions. Common use cases include the sliding window technique and merging sorted arrays.&lt;/p&gt;

&lt;h4&gt;
  
  
  Approach
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Initialize Two Pointers: Start with both pointers at the beginning of the data structure.&lt;/li&gt;
&lt;li&gt;Move the Pointers: Move one pointer (usually the faster one) ahead of the other based on specific conditions.&lt;/li&gt;
&lt;li&gt;Adjust the Pointers: Modify the positions of the pointers as needed to maintain the desired window conditions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;strong&gt;You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.&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;def mergeAlternately(word1: str, word2: str) -&amp;gt; str:
    # Initialize an empty list to store the merged characters
    word_list = []

    # Initialize two pointers, starting at the beginning of each word
    pointer_1 = 0
    pointer_2 = 0

    # Loop until one of the pointers reaches the end of its respective word
    while pointer_1 &amp;lt; len(word1) and pointer_2 &amp;lt; len(word2):
        # Append the character from word1 at pointer_1 to the list
        word_list.append(word1[pointer_1])
        # Append the character from word2 at pointer_2 to the list
        word_list.append(word2[pointer_2])

        # Move both pointers forward by one position
        pointer_1 += 1
        pointer_2 += 1

    # If there are remaining characters in word1, add them to the list
    if pointer_1 &amp;lt; len(word1):
        word_list.append(word1[pointer_1:])

    # If there are remaining characters in word2, add them to the list
    if pointer_2 &amp;lt; len(word2):
        word_list.append(word2[pointer_2:])

    # Join the list of characters into a single string and return it
    return "".join(word_list)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The two-pointer technique is a versatile and efficient tool in the world of algorithms, especially when dealing with arrays, strings, and linked lists. Whether the pointers are moving towards each other or in the same direction, this approach can simplify complex problems and improve the performance of your solutions. By understanding and applying these strategies, you'll be better equipped to tackle a wide range of coding challenges.&lt;/p&gt;

&lt;p&gt;I encourage you to practice these techniques by solving various problems and experimenting with different scenarios. With time and experience, you'll find the two-pointer technique to be an invaluable addition to your problem-solving toolkit.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Three ways of accessing string characters in JavaScript</title>
      <dc:creator>Charles Amakoye</dc:creator>
      <pubDate>Fri, 01 May 2020 05:22:19 +0000</pubDate>
      <link>https://forem.com/charlesamakoye/three-ways-of-accessing-string-characters-in-javascript-3gbn</link>
      <guid>https://forem.com/charlesamakoye/three-ways-of-accessing-string-characters-in-javascript-3gbn</guid>
      <description>&lt;p&gt;How do we access characters in a string? Well, in this post we look at three ways that we can use to access a character at a particular index, &lt;em&gt;i&lt;/em&gt;, in a string. In a string characters are indexed from left to right. For instance, in a string named &lt;em&gt;str&lt;/em&gt; the first character is at index 0, while the last is at index &lt;em&gt;str.length-1&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. using charAt() method
&lt;/h3&gt;

&lt;p&gt;This method will return the character at a specified index in a string. The method takes in a parameter, an integer that represents the index of the character to be returned. The syntax for usage is &lt;em&gt;string.charAt(index)&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = 'string';
console.log(str.charAt(0)); // s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If no character is found, the method returns &lt;em&gt;an empty string&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = 'string';
console.log(str.charAt(999)); // ''
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. using square brackets notation []
&lt;/h3&gt;

&lt;p&gt;Another way of accessing a character in a string is to using the square bracket. for example;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = 'string';
console.log(str[1]); // t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to access a character whose index is larger than the string length, the Square brackets [] returns &lt;em&gt;undefined&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = 'string';
console.log(str[999]); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. using for...of loop
&lt;/h3&gt;

&lt;p&gt;We can also access string characters by simply iterating over its characters using the for...of loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = 'string';
for(let char of str){
console.log(char); //s,t,r,i,n,g
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
