Strings are arrays/sequence/stream of characters and they are immutable. Strings are allocated one byte per character in memory
- Single quote string
jsx const message = 'Goodmorning'
- Double QUote string
jsx const message = "Hello"
- Template Literals
jsx const name = `Rose`
They are used to represent text and are represented by enclosing text or characters with quote as:
//string primitive
const str = "abcd"
const message = "hello";
// String object
const greetings = new String("hello")
const greetings = "Hello";
greetings.charAt(5)
//o
- indexOf() - check if a section exists in a string, if the section doesn't exists, it returns -1
const text = 'singletext'
single.indexOf('gle') // returns 3 (position)
- toUpperCase() - converts str to uppercase
const text = 'singletext'
single.toUpperCase()
- toLowerCase() - converts str to lower case
const text = 'singletext'
single.toLowerCase()
- substring() - fetches a section of the string
const text = 'singletext'
single.substring(2, 6) //
- includes() - returns true or false if the section of the string exists
const text = 'singletext'
single.includes('text') // true
- charAt() - accesses a string position
const text = 'singletext'
single.charAt(4) // l
- slice() - extracts a string position
const text = 'singletext'
single.slice(2, 5) // ngle
- splice() - replaces a section
const text = 'singletext'
single.splice(3, "o") // sinoletext
- split() - separates characters
const text = 'singletext'
single.split('') // s i n g l e t e x t
Access | Lookup | Insert(at) | Deletion At |
---|---|---|---|
O(1) | O(n) | O(n) | O(n) |
- Clarify about input char set and case sensitivity
- Usually the characters are limite to lowercase latin character, for example a-z
- Empty String
- String with 1 or 2 elements
- String with repeated characters
- Strings with only distinct characters
- This involves counting the frequency of characters in a string
- Common solution for that is using a hashmap
- For the sake of keeping a counter of characters, a common mistakes is to say that the space complexity required for the counter is O(n)
- The space required for a counter of a string of latin characters is O(1) not O(n)
Coolest trick to count characters in a string of unique chars is to use a 26-bit bitmask to indicate which lower case lation characters are inside the string
- This is a word switch
- The result of re-arranging the letters of a word to produce a new word, while using all the original letters only once. example: rat == tar, Earth == heart
- Sorting both strings should produce the same resulting string. O(nlogn)T O(logn) S
- Mapping each char to a prime number and we multiply each mapped number together anagrams should have the same multiple(prime factor decomposition) O(n) T O(1) S
- Frequency counting of characters will help determine if two strings are anagrams O(n) T O(1) S
This is a word, phrase, number or other sequence of characters which reads the same backwords as forward; example madam or racecar
- Reverse the string and it should be equal to itslef
- Have pointers at the start and end of the string P1 & P2.
move pointers inward till they meet
At ever point in time, the characters at both pointers should match
- When a question is about counting the number of palindromes, a common trick is to have two pointers that move outward, away from the middle
- For each middle pivot position, you need to check it twice-once that includes the characters and once without the character
- For substrings, you can terminate early once there is no match
- For subsequences, use dynamic programming as there are overlapping subproblems