{
  "slug": "reverse-string",
  "title": "Reverse a String",
  "category": "Strings",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Swap the first and last characters, then move both pointers inward. In-place, no second string required.",
  "code": [
    "lo = 0; hi = n-1",
    "while lo < hi:",
    "    swap(s[lo], s[hi])",
    "    lo++; hi--"
  ],
  "example": {
    "array": [],
    "text": "algorithm"
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 9,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Reverse \"algorithm\" in place.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        97,
        108,
        103,
        111,
        114,
        105,
        116,
        104,
        109
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "labels": [
        "a",
        "l",
        "g",
        "o",
        "r",
        "i",
        "t",
        "h",
        "m"
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "swap",
      "caption": "Swap 'a' and 'm'.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        109,
        108,
        103,
        111,
        114,
        105,
        116,
        104,
        97
      ],
      "focus": [
        "swap",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        "swap"
      ],
      "labels": [
        "m",
        "l",
        "g",
        "o",
        "r",
        "i",
        "t",
        "h",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 8,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "say",
      "caption": "Step both pointers inward.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        109,
        108,
        103,
        111,
        114,
        105,
        116,
        104,
        97
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "labels": [
        "m",
        "l",
        "g",
        "o",
        "r",
        "i",
        "t",
        "h",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 7,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "swap",
      "caption": "Swap 'l' and 'h'.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        109,
        104,
        103,
        111,
        114,
        105,
        116,
        108,
        97
      ],
      "focus": [
        null,
        "swap",
        null,
        null,
        null,
        null,
        null,
        "swap",
        null
      ],
      "labels": [
        "m",
        "h",
        "g",
        "o",
        "r",
        "i",
        "t",
        "l",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 7,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "say",
      "caption": "Step both pointers inward.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        109,
        104,
        103,
        111,
        114,
        105,
        116,
        108,
        97
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "labels": [
        "m",
        "h",
        "g",
        "o",
        "r",
        "i",
        "t",
        "l",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "Swap 'g' and 't'.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        109,
        104,
        116,
        111,
        114,
        105,
        103,
        108,
        97
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        null,
        null,
        "swap",
        null,
        null
      ],
      "labels": [
        "m",
        "h",
        "t",
        "o",
        "r",
        "i",
        "g",
        "l",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "say",
      "caption": "Step both pointers inward.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        109,
        104,
        116,
        111,
        114,
        105,
        103,
        108,
        97
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "labels": [
        "m",
        "h",
        "t",
        "o",
        "r",
        "i",
        "g",
        "l",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 3,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "swap",
      "caption": "Swap 'o' and 'i'.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        109,
        104,
        116,
        105,
        114,
        111,
        103,
        108,
        97
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        null,
        "swap",
        null,
        null,
        null
      ],
      "labels": [
        "m",
        "h",
        "t",
        "i",
        "r",
        "o",
        "g",
        "l",
        "a"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 3,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "settle",
      "caption": "Reversed end to end.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        109,
        104,
        116,
        105,
        114,
        111,
        103,
        108,
        97
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "labels": [
        "m",
        "h",
        "t",
        "i",
        "r",
        "o",
        "g",
        "l",
        "a"
      ],
      "markers": [],
      "regions": []
    }
  ]
}