# Longest Unique Substring of a string – Infytq 2019 Solve

```3
abcdabc
abababab
abAca
```

```abcd
-1
abAc
```

#### 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;
}

```

```3
abcdabc
abababab
abAca
```

```abcd
-1
abAc
```