Two Pointers

Leetcode 151 Reverse Words in a String

July 14, 2024
String, Two Pointers

Reverse Words in a String Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. ...

Leetcode 344 Reverse String, Leetcode 541 Reverse StringII

July 14, 2024
Two Pointers, String

344. Reverse String # Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Answer: func reverseString(s []byte) { n:=len(s) i,j:=0,n-1 for i<j { s[i],s[j]=s[j],s[i] i++ j-- } } I have learnt that: we do not need extra edge cases for reverse string 541. Reverse String II # Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. ...