Leetcode 100 Days Challenge Day 2 — Reversing Strings and Checking Powers of Two

Ahmed Islam
2 min readJun 2, 2024

--

AI-Generated Image

Greetings, programming enthusiasts! 🌟

Welcome back to the Leetcode 100 Days Challenge with a focus on Programming and DSA in Python. Today marks Day 2 of this exciting journey where I dived into challenging problems that not only honed my coding skills but also deepened my understanding of fundamental concepts. Let’s explore the highlights of my accomplishments today.

Day 2 Achievements

1. Reversing Strings

Challenge: Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in place with O(1) extra memory.

Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Effective Solution:
To tackle this challenge efficiently, I employed the two-pointer technique. By strategically positioning one pointer at the start of the array and another at the end, I successfully reversed the characters. Through a series of swaps and movement of pointers toward each other until meeting in the middle, I accomplished the task without utilizing additional memory.


class Solution:
def reverseString(self, s: List[str]) -> None:
i, j = 0, len(s) — 1
while i < j:
s[i], s[j] = s[j], s[i]
i, j = i + 1, j — 1

2. Power of Two Evaluation

Challenge: Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

Example 1:

Input: n = 1
Output: true
Explanation: 20 = 1

Effective Solution:
In addressing this challenge, I devised a loop mechanism to iteratively multiply a base value (initiated at 1) by 2 until it corresponds to `n` or surpasses it. By analyzing the matching conditions, I could decisively determine if `n` exhibited the characteristics of a power of two.


class Solution:
def isPowerOfTwo(self, n: int) -> bool:
power = 1
counter = 1
while power <= n:
if power == n:
return True
power = 2 ** counter
counter += 1
return False

Wrapping Up Day 2

That concludes the highlights of today’s endeavors! It’s immensely gratifying to witness forward momentum in skills acquisition and exploration of new complexities. I trust that you too are deriving value from this challenge. Stay tuned for forthcoming updates and insights as this shared journey unfolds.

To delve deeper into my journey and engage with me, visit my social media platforms:

For a continuous stream of enriching content, remember to follow me on Medium. Let’s persist in our coding efforts and collective growth! 🚀

--

--

Ahmed Islam

GDSC-VU Core Team 2023 - 24 | Tech Lead @ Web3 Pak | Data Science & Business Analytics intern @ The Sparks Foundation | IBM Certified Machine Learning