✔💯 DAY 416 | TWO pointers | 0ms 100% | | [PYTHON/JAVA/C++] | EXPLAINED 🆙🆙🆙
Intuition & Approach\n Describe your approach to solving the problem. \n##### \u2022\tThe string is converted to a char array so that the characters can be acce
# Intuition & Approach\n<!-- Describe your approach to solving the problem. -->\n##### \u2022\tThe string is converted to a char array so that the characters can be accessed more easily.\n##### \u2022\tTwo pointers are initialized to the beginning and end of the array.\n##### \u2022\tA variable is initialized to keep track of the number of operations performed.\n##### \u2022\tThe loop iterates until the left pointer reaches the right pointer.\n##### \u2022\tInside the loop, the characters at the left and right pointers are compared.\n##### \u2022\tIf the characters are not equal, then the smaller character is swapped with the larger character.\n##### \u2022\tThe number of operations is incremented by 1.\n##### \u2022\tIf the number of operations is 0, then the original string is returned.\n##### \u2022\tOtherwise, a new string is created from the char array and returned.\n\n\n\n# Complexity\n- Time complexity:o(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String makeSmallestPalindrome(String s) {\n    char[] arr = s.toCharArray();\n    int left = 0, right = arr.length - 1;\n    int ops = 0;\n    while (left < right) {\n        if (arr[left] != arr[right]) {\n            if (arr[left] > arr[right]) {\n                arr[left] = arr[right];\n            } else {\n                arr[right] = arr[left];\n            }\n            ops++;\n        }\n        left++;\n        right--;\n    }\n    if (ops == 0) {\n        return s;\n    }\n    return new String(arr);\n}\n}\n```\n# C++ \n```C++ \nstring makeSmallestPalindrome(string s) {\n  char arr[s.length()];\n  for (int i = 0; i < s.length(); i++) {\n    arr[i] = s[i];\n  }\n\n  int left = 0, right = s.length() - 1;\n  int ops = 0;\n  while (left < right) {\n    if (arr[left] != arr[right]) {\n      if (arr[left] > arr[right]) {\n        arr[left] = arr[right];\n      } else {\n        arr[right] = arr[left];\n      }\n      ops++;\n    }\n    left++;\n    right--;\n  }\n\n  if (ops == 0) {\n    return s;\n  }\n\n  string result = "";\n  for (int i = 0; i < s.length(); i++) {\n    result += arr[i];\n  }\n\n  return result;\n}\n\n```\n# PY \n\n```\ndef make_smallest_palindrome(s):\n  arr = list(s)\n  left = 0\n  right = len(arr) - 1\n  ops = 0\n  while left < right:\n    if arr[left] != arr[right]:\n      if arr[left] > arr[right]:\n        arr[left] = arr[right]\n      else:\n        arr[right] = arr[left]\n      ops += 1\n    left += 1\n    right -= 1\n\n  if ops == 0:\n    return s\n\n  result = ""\n  for i in arr:\n    result += i\n\n  return result\n```\n\n\n![BREUSELEE.webp](https://assets.leetcode.com/users/images/8bb0ed4d-d998-4778-8a6b-30bc8e97edfa_1684321488.7915804.webp)\n\n\n![meme2.png](https://assets.leetcode.com/users/images/d588f492-3f95-45f6-8e4a-10d6069002a5_1680054021.7117147.png)\n\nPlease Upvote\uD83D\uDC4D\uD83D\uDC4D\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n\u2B06 \u2B06 \u2B06 \u2B06 \u2B06 \u2B06 \u2B06 \u2B06 \u2B06
['Two Pointers', 'Python', 'C++', 'Java', 'Python3']