
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to form smallest number where no two adjacent digits are same in Python
Suppose we have a string s with four possible characters "1", "2", "3" and "?". We can place any one of "1", "2" and "3", in the place of "?". We have to find the smallest possible number that we can make such that no two adjacent digits are same.
So, if the input is like s = "2??3?", then the output will be 21231
To solve this, we will follow these steps −
- i := 0
- s := a list of elements from s
- if size of s < 2, then
- if s[i] is same as "?", then
- return "1"
- if s[i] is same as "?", then
- while i < size of s, do
- if s[i] is same as "?", then
- if i is same as 0, then
- s[i] := "1" when s[i + 1] is not "1" otherwise "2"
- otherwise when i > 0 and i <= size of s - 2, then
- if s[i - 1] is same as "1", then
- if s[i + 1] is same as "2", then
- s[i] := "3"
- otherwise,
- s[i] := "2"
- if s[i + 1] is same as "2", then
- otherwise when s[i - 1] is same as "2", then
- if s[i + 1] is same as "2", then
- s[i] := "3"
- otherwise,
- s[i] := "1"
- if s[i + 1] is same as "2", then
- otherwise when s[i - 1] is same as "3", then
- if s[i + 1] is same as "2", then
- s[i] := "2"
- otherwise,
- s[i] := "1"
- if s[i + 1] is same as "2", then
- if s[i - 1] is same as "1", then
- otherwise,
- s[i] := "1" when s[i - 1] is not "1" otherwise "2"
- if i is same as 0, then
- i := i + 1
- if s[i] is same as "?", then
- join items of s into a string and return
Example
Let us see the following implementation to get better understanding −
def solve(s): i = 0 s = list(s) if len(s) < 2: if s[i] == "?": return "1" while i < len(s): if s[i] == "?": if i == 0: s[i] = "1" if s[i + 1] != "1" else "2" elif i > 0 and i <= len(s) - 2: if s[i - 1] == "1": if s[i + 1] == "2": s[i] = "3" else: s[i] = "2" elif s[i - 1] == "2": if s[i + 1] == "1": s[i] = "3" else: s[i] = "1" elif s[i - 1] == "3": if s[i + 1] == "1": s[i] = "2" else: s[i] = "1" else: s[i] = "1" if s[i - 1] != "1" else "2" i += 1 return "".join(s) s = "2??3?" print(solve(s))
Input
"2??3?"
Output
21231
- Related Articles
- Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python
- Program to check whether we can color a tree where no adjacent nodes have the same color or not in python
- Rearrange characters in a string such that no two adjacent are same in C++
- Program to find nearest number of n where all digits are odd in python
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Program to separate persons where no enemies can stay in same group in Python
- Program to find number not greater than n where all digits are non-decreasing in python
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Program to count n digit integers where digits are strictly increasing in Python
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Program to find a sublist where first and last values are same in Python
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- 8085 program to find smallest number between two numbers

Advertisements