Problem: Find the longest substring of unique characters which is case insensitive. Minimum length of the substring should be 3. If the criteria doesn’t match then returns -1.
Input Format: First line contains a single integer n, the number of testcases. Next n line contains n strings as input.
(NOTE: The actual problem didn’t have the number of testcases as input. But for your better convenience we have worked with testcases.)
Sample Input:
3
abcdabc
abababab
abAca
Sample Output:
abcd
-1
abAc
Explanation: Here in the first example, the longest substring that can be made keeping the criteria, is “abcd” which means the first longest substring made will be taken into consideration.
Here in the second case, the longest unique substring that can be made is “ab” but it doesn’t fulfil the criteria. Hence prints -1.
Lastly, the longest unique substring that can be made is “abAc” since its case insensitive ‘A’ is allowed and will be counted as a unique character.
Solution 1: We strongly recommend you to try the problem first before moving to the solution.
Python
# Solution of longest unique even integer problem in Python
# www.codewindow.in
def solve(str):
length=len(str)
unique=''
for i in range (length):
substr=str[i]
for j in range (i+1, length):
substr += str[j]
sub_len=len(substr)
if sub_len>=3 and len(set(substr))==sub_len:
if len(unique)<sub_len:
unique=substr
if len(unique)==0:
print("-1")
else:
print(unique)
# Driver Code
test_case=int(input()); # Take number of testcases
while(test_case):
str=input()
solve(str)
test_case-=1
C++
#include <bits/stdc++.h>
#define ll long long
#define all(c) (c).begin(), (c).end()
using namespace std;
void solve() {
string s, temp;
cin >> s;
for(int i=0; i<s.length(); ++i) {
bool ok=1;
temp=s.substr(0, i);
// substr(size_t starting_pos, size_t length_of_substring)
for(int j=0; j<=i; ++j) {
if(count(all(temp), temp[i-1])>1) {
// Count the frequency of each character we are adding up.
temp.pop_back();
// If frequency of that character is > 1 then pop that character from string
ok=0;
// Turn the boolean into 0. Used for flag.
break;
// Break the entire process
}
}
if(ok==0)
// We have reached the maximum limit and we don't want to continue.
break;
}
if(temp.length()<3)
cout << "-1" << "\n";
else
cout << temp << "\n";
}
// Driver Code
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
cin >> t; // Testcases
while(t--)
solve();
return 0;
}
Input:
3
abcdabc
abababab
abAca
Output:
abcd
-1
abAc
Follow Us
You Missed
- UKG Hiring for Software Engineer
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- Programming in Python – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Data Structure – comprehensive guide – codewindow.in