Don't Just Code
Learn To Solve
Problems
Your step by step roadmap to landing your dream tech job.
Join AlgoEspresso now and start learning for Free.
Join AlgoEspresso now and start learning for Free.

Great Structured Learning Paths
AlgoEspresso

esc
F1
F2
F3
F4
F5
F6
F7
F8
F8
F10
F11
F12
~`
!1
@2
#3
$4
%5
^6
&7
*8
(9
)0
—_
+ =
delete
tab
Q
W
E
R
T
Y
U
I
O
P
{[
}]
|\
caps lock
A
S
D
F
G
H
J
K
L
:;
"'
return
shift
Z
X
C
V
B
N
M
<,
>.
?/
shift
fn
control
option
command
command
option
Interactive Lessons
1class Solution:
2 def longestNiceSubstring(self, s: str) -> str:
3 freq = Counter(s)
4 index = 0
5
6 while index < len(s):
7 if not freq[s[index].lower()] or not freq[s[index].upper()]:
8 break
9 index += 1
10
11 # current string is nice
12 if index == len(s):
13 return s
14
15 return max(
16 self.longestNiceSubstring(s[:index]),
17 self.longestNiceSubstring(s[index+1:]),
18 key=len
19 )
20