Mastery Points
0

String Traversal Logic in dsa

Context & Logic

String traversal involves visiting each character in the string one by one. This is the foundation for solving most string problems like reversing, counting characters, or finding palindromes.

Example

const str = "Punam";
for (let i = 0; i < str.length; i++) {
    console.log(str[i]);
}
// Using for...of
for (let char of str) {
    console.log(char);
}

Step-by-Step Logic

1

Start a loop from index 0 to length - 1.

2

Access the character at the current index.

3

Perform the required operation (e.g., check for vowels, add to a result string).

Complexity Metrics

Time Efficiency

O(n)

Memory Footprint

O(1) (unless storing results)