String Split

Without using a built-in string split method, implement a split(s, c) method, which receives a string s and a character c and splits s at each occurrence of c, returning a list of strings.

Examples:

  • s = "split by space", c = ' '
    • Output: ["split", "by", "space"]
  • s = "beekeeper needed", c = 'e'
    • Output: ["b", "", "k", "", "p", "r n", "", "d", "d"]
  • s = "/home/../Documents/", c = '/'
    • Output: ["", "home", "..", "Documents", ""]
  • s = "", c = '?'
    • Output: []
1def split(s: str, c: str) -> List[str]:
2 res = []
3 current = []
4 for char in s:
5 if char == c:
6 res.append("".join(current))
7 current = []
8 else:
9 current.append(char)
10 res.append("".join(current))
11 return res
a
0
1
b
2
3
c
4
s = "a b c"
s=a b c
c=
res=[]
current=
Step 1 / 7
Step 1:
Initialize result list and current buffer
Focus: default
s="a b c"c=" "current=""