DEV Community

Cover image for Data Structure String In JavaScript | Frontend Interview Helper
Sameer Faridi
Sameer Faridi

Posted on

Data Structure String In JavaScript | Frontend Interview Helper

The basics | Inbuilt Function of Js

How to declare a String in JS

Accessing Characters

Image description

Accessing Characters

Image description

Looping a string

Image description

Modifying Strings

Image description

Concat & Trim (Trim has only removed side spaces | didn't remove space in between )

Image description

Searching in String

Image description

Took a part from a string ( Slice, Subdtring)

Image description

Convert Case ( Str -> number | Num -> Str | etc..)

This is important because if we Typecast an Object in Js it gives a [ Object Object ] simple mines gives us its type

Image description

Image description
The Right way to do that

Image description

Image description

Convert a String

Image description

Check Ascii value or Convert to Ascii Value

Image description

String Compare

Image description
it returns 0 if it is the same otherwise -1

To check whether something is included in String or not

Image description

It returns True | False values

Splitting and Joining Strings

Image description
It breaks the string from " " (space) occurs

Image description

Finally The Main Data Structure & Algorithm Questions Start.

Image description

My Solution:

`const mainString = ' Hay Good Morning Brother';
const maxLenght = 10;

if (mainString.length >= maxLenght) {
let trimedvalue = mainString.trim();
let finalVersionOfString = trimedvalue.replaceAll(" ", "");
console.log(${finalVersionOfString.slice(0, 10)}...);
} else {
console.log(mainString);
}
// Output : HayGoodMor...`

Piyush's Solution:

Image description

Q2: Plaindrome:

Image description

My Solution:

Image description

I add comments with ChatGPT (I think it is a better approach because sometimes interviewers don't allow to use inbuilt functions of a language )

quick Question

Can Someone Explain?
Hey, I tried my best and approached this question in five different ways using JavaScript. The average runtime is around 140ms. However, when I attempted the same question in Java, it only took around 9ms. How is this possible? Why is there such a significant difference? Could someone please explain?

Image description

Question 03: 461. Hamming Distance

Image description
A quick tip: Convert a number to binary just | let num = 10; num.toString(2) --> that 2 refers base^2
I don't know why on leetcode in this program time limit exceeds.

Image description

Here is the optimized Solution.

Image description

Question 04: 242. Valid Anagram

Image description

Solution: (UnOptimized ) Easy one:

Image description

Solution: (Optimized ):

Image description

Bonus:

Top 10 most asked frontend interview questions from string

Reverse a String

Check Palindrome

String Anagrams

Longest Substring Without Repeating Characters

String Compression

Implement strStr()

Valid Parentheses

Longest Common Prefix

Group Anagrams

Count and Say

Thank you So Much For Reading if you get some knowledge from this BLOG Connect with me on LinkedIn

LinkedinFollow
GithubFollow

Made by Lust Love By Sameer Faridi (21 April 2023)

Top comments (0)