Lexicographically Smallest String After a Swap

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
Given a string s containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s with the same parity at most once.
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not.
LeetCode Problem - 3216
class Solution {
public String getSmallestString(String s) {
// Get the length of the input string
int length = s.length();
// Convert the input string to a character array
char[] charArray = s.toCharArray();
// Iterate through the character array except the last character
for (int i = 0; i < length - 1; i++) {
// Convert current character and next character to integers
int currentInt = charArray[i] - '0';
int nextInt = charArray[i + 1] - '0';
// Check if both characters are even or odd and if the current character is greater than the next
if ((currentInt % 2 == nextInt % 2) && (currentInt > nextInt)) {
// Swap the current character with the next character
char temp = charArray[i];
charArray[i] = charArray[i + 1];
charArray[i + 1] = temp;
break; // Exit the loop after the first swap
}
}
// Convert the character array back to a string and return it
return new String(charArray);
}
}




