
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Longest possible string built from two strings in JavaScript
Problem
We are required to write a JavaScript function that takes in two strings s1 and s2 including only letters from ato z.
Our function should return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.
Example
Following is the code −
const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', str2 = '') => { const combined = str1.concat(str2); const lower = combined.toLowerCase(); const split =lower.split(''); const sorted = split.sort(); const res = []; for(const el of sorted){ if(!res.includes(el)){ res.push(el) } } return (res.join('')); }; console.log(longestPossible(str1, str2));
Output
Following is the console output −
abcdefklmopqwxy
- Related Articles
- Longest string consisting of n consecutive strings in JavaScript
- Find the Length of the Longest possible palindrome string JavaScript
- Longest string with two distinct characters in JavaScript
- Finding all the longest strings from an array in JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program
- Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions
- Construct objects from joining two strings JavaScript
- How to find the longest common substring from more than two strings in Python?
- Find the longest common prefix between two strings after performing swaps on second string in C++
- Count of sub-strings of length n possible from the given string in C++
- C# Program to find the longest string from an array of strings using Lambda Expression
- Length of longest string chain in JavaScript
- All possible strings of any length that can be formed from a given string?
- Length of the longest possible consecutive sequence of numbers in JavaScript

Advertisements