# [3216\. Lexicographically Smallest String After a Swap](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/)
:::spoiler Hint
```cpp=
class Solution {
public:
string getSmallestString(string s)
{
// Iterate through the string from the beginning to the second last character
for ()
{
// Check if both current and next characters have the same parity (both even or both odd)
// and if the current character is greater than the next character
if ()
{
// Swap the current character with the next one
// Exit the loop after the first swap
}
}
// Return the modified string
}
};
```
:::
:::spoiler Solution
```cpp=
class Solution {
public:
string getSmallestString(string s)
{
for (int i = 0; i < s.size() - 1; ++i)
{
if (s[i] % 2 == s[i + 1] % 2 && s[i] > s[i + 1])
{
swap(s[i], s[i + 1]); break;
}
}
return s;
}
};
```
- 時間複雜度:$O(n)$
- 空間複雜度:$O(1)$
:::