January 11, 2022

Really its better served as a good example of reverse array done a little faster. The interesting thing is that its a reverse array using two pointers (indexes). The real thought as to why its more effective is that every iteration of the loop is a logical if/then. So in order to cut down on every logic check cycle we can issue multiple changes per cycle, something that seems like it would be an obvious answer to speeding up the function.

Link LeetCode Link

alt text

var reverseString = function(s) { let start = 0; let end = s.length - 1; while(start<end){ let tmp = s[end]; s[end] = s[start]; s[start] = tmp; start++; end--; } };

I thought it was a neat way to illustrate how a little foresight can go a long way in optimizing solutions.