STRING_S - Editorial

PROBLEM LINK:

Practice
Contest

Author: Prathamesh Sogale
Tester: Prathamesh Sogale
Editorialist: Ram Agrawal

DIFFICULTY:

EASY

PREREQUISITES:

Basic loops and string manipulation

PROBLEM:

Meena and his friend are busy solving problems. One day his teacher allotted an assignment. In order to save time they decided to solve the assignment together . While solving the assignment they realised that they had forgot to submit the assignments of other subjects. As a result, they decided that one of them would complete a specific subject assignment while the other would copy it. Meena is clever and wants to avoid the work so he told his friend that he will solve only the assignment of that subject if no character is repeated in the name of that subject.

  • You are given a String SS denoting the subject, you need to determine if Meena needs to solve the assignment of that subject or not.
  • String will not contain any special symbol or numeric character.
  • Print “YES” if he needs to write the assignment else print “NO”.

QUICK EXPLANATION:

In this problem, we have to check if any character of the string is repeated or not. If repeated then we have to print “NO” else print “YES”.

SOLUTIONS:

Setter's Solution
T=int(input())
for i in range(T):
str=input()
str=str.lower()
lst=[]
s=set()
for i in str:
lst+=i.split()
for i in lst:
if i not in s:
s.add(i)
if len(lst) == len(s):
print(YES)
else:
print(NO)
Tester's Solution
// CPP program to find the first
// repeated character in a string
#include <bits/stdc++.h>
#include
using namespace std;
// Returns first repeating character in str.
char firstRepeating(string &str)
{
// Creates an empty hashset
unordered_set h;
// Traverse the input array from left to right
for (int i=0; i<str.length(); i++)
{
char c = str[i];
// If element is already in hash set, update x
// and then break
if (h.find(c) != h.end())
return c;
else // Else add element to hash set
h.insert(c);
}
// If there was no repeated character
return ‘\0’;
}
// Driver method to test above method
int main ()
{
int t;
cin >> t;
while(t–){
string str;
cin >> str;
transform(str.begin(),str.end(),str.begin(), ::tolower);
if(firstRepeating(str)==’\0’){
cout<< YES <<endl;
}
else
{
cout << NO << endl;
}
}
return 0;
}