Skip to content

Commit

Permalink
string/reverse-string
Browse files Browse the repository at this point in the history
  • Loading branch information
aolenevme authored Nov 18, 2021
1 parent 9026442 commit 6c38ebe
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions string/reverse-string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
1 <= s.length <= 105
s[i] is a printable ascii character.
*/

package main

import "fmt"

func main() {
reverseString([]byte(string([]rune{'h', 'e', 'l', 'l', 'o'})))
}

func reverseString(s []byte) {
start, end := 0, len(s)-1
for start <= end {
tmp := s[start]
s[start] = s[end]
s[end] = tmp

start++
end--
}

fmt.Println(string(s))
}

0 comments on commit 6c38ebe

Please sign in to comment.