Foundation·Concept·2 min read

Arrays and strings in Python

Practice this question

Problems to practice

  • Two Sum
  • Best Time to Buy and Sell Stock
  • Move Zeroes
  • Product of Array Except Self
  • Valid Palindrome
  • Longest Substring Without Repeating Characters
  • Group Anagrams

Example: two sum

Python
def two_sum(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        needed = target - num
        if needed in seen:
            return [seen[needed], i]
        seen[num] = i
    return []

Time and space complexity

  • Time: O(n)
  • Space: O(n)

Adapted from 18_DSA_PYTHON.md · “2. Arrays and strings in Python”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.