LeetCode: Longest Nice Substring Solution
Approach
Brute-force
Implementation
1/**2 * @param {string} s3 * @return {string}4 */5var longestNiceSubstring = function (s) {6 s = s.split("")7 const N = s.length8 let max = ""910 for (let i = 0; i < N - 1; i++) {11 let substr = [s[i]]1213 for (let j = i + 1; j < N; j++) {14 substr.push(s[j])15 let isNice = true1617 for (const c of substr) {18 if (19 !substr.includes(c.toLowerCase()) ||20 !substr.includes(c.toUpperCase())21 ) {22 isNice = false23 }24 }2526 if (isNice && substr.join("").length > max.length) {27 max = substr.join("")28 }29 }30 }3132 return max33}
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