A string is actually an array of unicode characters. You can perform almost all the operations we used in an array.

However, there are some differences. In this article, we will go through some of them which you should be aware of when dealing with a string. These features might vary a lot from one language to another.



Add Binary

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

1
2
Input: a = "11", b = "1"
Output: "100"

Example 2:

1
2
Input: a = "1010", b = "1011"
Output: "10101"

LeetCode Link


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
const addBinary = function(a, b) {
// Gets the bigger length of the two strings
const maxLength = Math.max(a.length, b.length);

// Pads strings to make them have the same length
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
a = a.padStart(maxLength, '0');
b = b.padStart(maxLength, '0');

let carry = 0;
let sum = 0;
let result = '';

for(let i = maxLength - 1; i >= 0; i--){
sum = carry + a.charAt(i)*1 + b.charAt(i)*1;
result = sum % 2 + result;

// Gets the integer part
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
carry = Math.trunc(sum / 2);
}

if(carry > 0){
return carry + result;
}

return result;
};

Implement strStr()

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

1
2
Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

1
2
Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string.
This is consistent to C’s strstr() and Java’s indexOf().

LeetCode Link


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
const strStr = function(haystack, needle) {
// Validates the haystack
if(haystack === null || haystack === undefined){
return -1;
}

return haystack.indexOf(needle);
};


Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

Example 1:

1
2
Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

1
2
3
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

LeetCode Link


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const longestCommonPrefix = function(strs) {

// Initializes the result
let result = "";

// Validates the parameter
if(!strs || strs.length === 0)
{
return result;
}

// Copies the array of strings because we will change the original array (This is optional)
const strings = strs;

// Gets the first string of the array
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
const firstStr = strings.shift();

// Initializes the index and the index value
let index = 0;
let indexVal = "";

// Loops over the the rest of the array
// The loop breaks when the index moves to the end of the first string or a different charecter is found
while(true){

// Gets the character of the first string by index
indexVal = firstStr.charAt(index);

// Checks if all the strings have the same character at same index
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
if( indexVal === "" || !strings.every( str => indexVal === str.charAt(index) ) )
{
break;
}
else{

// Adds the index value to the result
result = result.concat(indexVal);

// Increases the index
index++;
}

}

return result
};