Reorganize String
Given a string s
, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
If possible, output any possible result. If not possible, return the empty string.
Example 1:
Input:s = "aab"
Output: aba
Example 2:
Input:s = "aaab"
Output: ``
Note:
s
will consist of lowercase letters and have length in the range [1, 500]
.
Try it yourself
xxxxxxxxxx
import sys
from typing import Counter
​
def reorganize_string(s: str) -> str:
# WRITE YOUR BRILLIANT CODE HERE
return ""
​
if __name__ == "__main__":
s = input()
res = reorganize_string(s)
if not res:
print("Impossible")
sys.exit()
input_counter, output_counter = Counter(s), Counter(res)
if input_counter != output_counter:
print("Not rearrangement")
sys.exit()
for i in range(len(res) - 1):
if res[i] == res[i + 1]:
print(f"Same character at index {i} and {i+1}")
sys.exit()
print("Valid")
​
Title
Script
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Contrary to popular belief, Lorem
Ipsum
is not simply random text.
>>> a = [1, 2, 3] >>> a[-1] 3
xxxxxxxxxx
from typing import List
def testFunction(arr: List[int], target) -> int:
# WRITE YOUR BRILLIANT CODE HERE
​
if __name__ == '__main__':​
​
Get AlgoMonster Pro for instant access to all content and solutions