String Split and Join

Sort by

recency

|

1094 Discussions

|

  • + 0 comments

    A Better Approach

    def split_and_join(line): # write your code here return line.replace(" ", "-")

    if name == 'main': line = input() result = split_and_join(line) print(result)

  • + 0 comments

    Absolutely! In Python, using method is a simple and powerful way to break a string into a list based on a specified delimiter. My fairplay

  • + 0 comments

    My brute force solution: Time Complexity : O(n^2) Space Complexity : O(n)

    def split_and_join(line):
        # write your code here
        result = ""
        
        for i in line:
            if i.isspace():
                result += "-"
            else:    
                result += i
        
        return result
    
    if __name__ == '__main__':
        line = input()
        result = split_and_join(line)
        print(result)
    

    Better optimization solutions

    def split_and_join(line):
        result = []
        for i in line:
            if i.isspace():
                result.append("-")
            else:
                result.append(i)
        return "".join(result)
    

    Time Complexity: O(n) Space Complexity: O(n)

  • + 0 comments

    def split_and_join(line):

    a=input() print("-".join(a.split())) if name == 'main': line = input() result = split_and_join(line) print(result)

        i am getting error even after writing code because of deafult code present in it.
    
  • + 0 comments

    s=input(); s=s.replace(' ','-'); print(s)