Header Ads Widget

Username Changes | Hackerrank certification question solution

Username Changes | Hackerrank  certification question solution  

A company has released a new internal system, and each employee has been assigned a username. Employees are allowed to change their usernames but only in a limited way. More specifically, they can choose letters at two different positions and swap them. For example, the username "bigfish" can be changed to "gibfish" (swapping 'b' and 'g') or "bighisf" (swapping 'f' and 'h'). The manager would like to know which employees can update their usernames so that the new username is smaller in alphabetical order than the original username.

For each username given, return either "YES" or "NO" based on whether that username can be changed (with one swap) to a new one that is smaller in alphabetical order.

Note: For two different strings A and B of the same length, A is smaller than B in alphabetical order when on the first position where A and B differ, A has a smaller letter in alphabetical order than B has.

For example, let's say usernames = ["bee", "superhero", "ace"]. For the first username, "bee", it is not possible to make one swap to change it to a smaller one in alphabetical order, so the answer is "NO". For the second username, "superhero", it is possible get a new username that is smaller in alphabetical order (for example, by swapping letters 's' and 'h' to get "hupersero"), so the answer is "YES". Finally, for the last username "ace", it is not possible to make one swap to change it to a smaller one in alphabetical order, so the answer is "NO". Therefore you would return the array of strings ["NO", "YES","NO"]. 


Function Description

Complete the function possibleChanges in the editor below.
   possibleChanges has the following parameter(s):
          string usernames[n]. an array of strings denoting the usernames of the employees Returns:
           string[n]: an array of strings containing either "YES" or "NO" based on whether the username can be changed with one swap to a new one that is smaller in alphabetical order

Constraints

  • 1≤ n ≤105
  • The sum of lengths of all usernames does not exceed 106.
  • usernames[i] consists of only lowercase English letters.

Username Changes | Hackerrank  certification question solution  

The objective of the code is For each username given, return either "YES" or "NO" based on whether that username can be changed (with one swap) to a new one that is smaller in alphabetical order.

Sample Case 1

Sample Input For Custom Testing

3
foo
bar
baz

Sample Output:-
NO
YES
YES

Explanation

There are three usernames to consider in this case. For the first of them, "foo", it is not possible to make one swap to change it to a smaller one in alphabetical order, so the answer is "NO" (without quotes). For the second one, "bar", one can swap the letters "b" and "a" to get the new username "abr", which is smaller in alphabetical order, so the answer is "YES" (without quotes). Similarly, For the third username, "baz", one can swap the letters "b" and "ab" to get the new username "abz", which is smaller in alphabetical order, so the answer is "YES" (without quotes).


Code:-

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);


// function for finding possible changes
vector<string> possibleChanges(vector<string> usernames) {
int n=usernames.size();
vector<string>ans;
for(int i=0;i<n;i++)
{
string s=usernames[i];
int l=s.length();
int f=0;
for(int j=0;j<l;j++)
{
for(int k=j+1;k<l;k++)
{
if(s[j]>s[k])
{
ans.push_back("YES");
f=1;
break;
}
}
if(f==1)
break;
}
if(f==0)
ans.push_back("NO");
}
return ans;

}


//Driver function
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string usernames_count_temp;
getline(cin, usernames_count_temp);

int usernames_count = stoi(ltrim(rtrim(usernames_count_temp)));

vector<string> usernames(usernames_count);

for (int i = 0; i < usernames_count; i++) {
string usernames_item;
getline(cin, usernames_item);

usernames[i] = usernames_item;
}

vector<string> result = possibleChanges(usernames);

for (int i = 0; i < result.size(); i++) {
fout << result[i];

if (i != result.size() - 1) {
fout << "\n";
}
}

fout << "\n";

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-







Post a Comment

0 Comments