Github: JavaScript Problems by Mitchell.
All the following code blocks use TypeScript.
Alphanumericals
Determine if a character an alphanumerical.
Solutions
Use regular expression to match pattern.
Code
function isAlphaNumeric(char: any): Boolean {
return /[A-Za-z0-9]/.test(char);
}
Compare Version Numbers
Given two version strings,
version1
andversion2
, compare them. A version string consists of revisions separated by dots'.'
. The value of the revision is its integer conversion ignoring leading zeros.To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as
0
.Return the following:
If
version1 < version2
, return -1.If
version1 > version2
, return 1.Otherwise, return 0.
-LeetCode
Solutions
Use `split()
` to split the string into array. Compare each char in the array.
Code
const compareVersion = function (version1: string, version2: string): number {
const v1Arr = version1.split('.');
const v2Arr = version2.split('.');
const maxLen = Math.max(v1Arr.length, v2Arr.length);
for (let i = 0; i < maxLen; i++) {
const v1Char = parseInt(v1Arr[i]) || 0;
const v2Char = parseInt(v2Arr[i]) || 0;
if (v1Char > v2Char) {
return 1;
} else if (v1Char < v2Char) {
return -1;
} else {
continue;
}
}
return 0;
}
Version Numbers Sortings
Sorting a given array of version numbers.
Solutions
The solution is similar to Compare Version Numbers.
Code
const versionSorting = function (versionArr: string[]): string[] {
versionArr.sort((a, b) => {
const arr1 = a.split('.');
const arr2 = b.split('.');
let i = 0;
while (true) {
const s1: number = parseInt(arr1[i]);
const s2: number = parseInt(arr2[i]);
i++;
if (s1 === undefined || s2 === undefined) {
return arr2.length - arr1.length;
}
if (s1 === s2) {
continue;
}
return s2 - s1;
}
});
return versionArr;
}
most frequently occurring character
Given a non-empty string, return the most frequently ocurring character.
If there are multiple characters with same occurrance, return an array of them. - BFE
Solutions
Use a `Map()
` to record the frequency of each character.
Code
const count = function (str: string): string | string[] {
const map = new Map();
const result: string[] = [];
for (const char of str) {
map.set(char, (map.get(char) || 0) + 1);
}
const max = Math.max(...map.values());
for (const [key, value] of map) {
if (value === max) {
result.push(key);
}
}
return result.length === 1 ? result[0] : result;
}
Find Longest Word Length
Find the longest word length in a string.
Solutions
Use `split(‘ ‘)
` to split the string and find the longest word length.
Code
const longestLength = function (str: string): number {
return Math.max(
...str
.split(' ')
.map(x => x.length)
);
}
Find Word With Longest Length
Find the word with longest length in a string.
Solutions
First find the max length of the words in string. Then match the word with longest length.
Code
const longestWord = function (str: string): string[] {
const wordArr = str.split(' ');
const result: string[] = [];
let max = 0;
for (const word of wordArr) {
if (word.length > max) {
max = word.length;
}
}
for (const word of wordArr) {
if (word.length === max) {
result.push(word);
}
}
return result;
}