Knowee
Questions
Features
Study Tools

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. Example 1:Input: s = "45320"Output: "43520"Explanation:

Question

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. Example 1:Input: s = "45320"Output: "43520"Explanation:

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution to the problem:

  1. Initialize a variable i to 0. This variable will be used to iterate over the string s.

  2. Start a while loop that continues until i is less than the length of s minus 1. This is because we are comparing each digit with its adjacent digit, so we don't need to check the last digit.

  3. Inside the loop, check if the current digit and the next digit have the same parity. You can do this by converting each digit to an integer, taking the modulus 2 of each, and checking if they are equal. If they are, this means they are both even or both odd.

  4. If the current digit is greater than the next digit, swap them. You can do this by slicing the string to get the part before the current digit, the next digit, the current digit, and the part after the next digit, and concatenating these parts together.

  5. If a swap was made, break the loop. This is because we can only make at most one swap.

  6. If no swap was made, increment i by 1 to move on to the next digit.

  7. After the loop, return the string s. This will be the lexicographically smallest string that can be obtained after swapping adjacent digits with the same parity at most once.

Here is a Python code snippet that implements this solution:

def smallestString(s):
    i = 0
    while i < len(s) - 1:
        if int(s[i]) % 2 == int(s[i+1]) % 2 and s[i] > s[i+1]:
            s = s[:i] + s[i+1] + s[i] + s[i+2:]
            break
        i += 1
    return s

For example, if you call smallestString("45320"), it will return "43520".

This problem has been solved

Similar Questions

You are given a string s.Your task is to remove all digits by doing this operation repeatedly:Delete the first digit and the closest non-digit character to its left.Return the resulting string after removing all digits. Example 1:Input: s = "abc"Output: "abc"Explanation:There is no digit in the string.Example 2:Input: s = "cb34"Output: ""Explanation:First, we apply the operation on s[2], and s becomes "c4".Then we apply the operation on s[1], and s becomes "". Constraints:1 <= s.length <= 100s consists only of lowercase English letters and digits.The input is generated such that it is possible to delete all digits.

Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string.Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string.(*>#): positive integer(#>*): negative integer(#=*): 0Example 1:Input 1:###*** -> Value of SOutput :0 ? number of * and # are equal

Digit SeparationA Competition is scheduled in Buddy4Study Primary School so that the KG students can get some appreciation in understanding odd and even numbers. The task is to identify the even digits which are both preceded and followed by an odd digit. As the number of student registrations is high, your help in this regard is highly expected. Develop a python program for generating the output as shown in sample input and output. The first and last digits cannot be included in output, as they donot have preceding digit and following digit, respectively. If no such digit is found, print "NIL".Sample Input:56474182729Sample Output: (Display output digits, each in a separate line)42

Given two integers s and d. The task is to find the smallest number such that the sum of its digits is s and the number of digits in the number are d. Return a string that is the smallest possible number. If it is not possible then return -1.

How many different nine digit numbers can be formed from the number 223355888 by rearranging its digits so that the odd digits occupy even positions?

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.