This Python program takes a list of usernames as input and checks if it's possible to rearrange the characters in each username in a way that the new username would be lexicographically smaller than the original one. If it's possible, the program returns 'YES', otherwise it returns 'NO'. The program uses a simple string comparison approach to solve the problem.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by iterating over each username in the list. For each username, it checks if there is a character that is lexicographically greater than the next character. If such a character is found, it means that by swapping these two characters, a lexicographically smaller username can be obtained. Therefore, the program appends 'YES' to the answer list. If no such character is found, it means that the username is already in the smallest lexicographical order, so the program appends 'NO' to the answer list. The program returns the answer list as the final result.
import math
import os
import random
import re
import sys
def possibleChanges(usernames):
ans = []
for u in usernames:
if len(u) <= 1:
ans.append("NO")
for i in range(len(u) - 1):
if u[i] > u[i + 1]:
ans.append("YES")
break
else:
ans.append("NO")
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
usernames_count = int(input().strip())
usernames = []
for _ in range(usernames_count):
usernames_item = input()
usernames.append(usernames_item)
result = possibleChanges(usernames)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()
If you have a HackerRank Certification test or a coding interview coming up, you can use CodeRankGPT to your advantage. It will assist you during your test and help ensure you get the certification or the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.