LeetCode: Longest Nice Substring Solution

Approach

Brute-force

Implementation

1/**
2 * @param {string} s
3 * @return {string}
4 */
5var longestNiceSubstring = function (s) {
6 s = s.split("")
7 const N = s.length
8 let max = ""
9
10 for (let i = 0; i < N - 1; i++) {
11 let substr = [s[i]]
12
13 for (let j = i + 1; j < N; j++) {
14 substr.push(s[j])
15 let isNice = true
16
17 for (const c of substr) {
18 if (
19 !substr.includes(c.toLowerCase()) ||
20 !substr.includes(c.toUpperCase())
21 ) {
22 isNice = false
23 }
24 }
25
26 if (isNice && substr.join("").length > max.length) {
27 max = substr.join("")
28 }
29 }
30 }
31
32 return max
33}

Comments

Loading comments...

Tags

leetcode

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Broken Calculator

Feb 21, 2021

HoningJS

Search Posts