-1

What is the significance of ("") before join method?? What I know is that we use "".join. But here the code is working fine if though ("") is used. Someone please explain.

("").join([map_s_to_t[s[i]] for i in range(len(s))])

Following is my whole code...

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        if len(set(s)) != len(set(t)):
            return False
    
        # Create a mapping of chars from  s -> t
        map_s_to_t = { s[i]: t[i] for i in range(len(s)) }
    
        # Create a new string by replacing the chars in s with t
        s_replaced_with_t = ("").join([map_s_to_t[s[i]] for i in range(len(s))])
        
        # if the replaced string is same as t , then True else False    
        return(s_replaced_with_t == t)
Veer Sharma
  • 15
  • 1
  • 3
  • ("").join tells to concatinate strings with nothing in between. For exmaple '-'.join(['hello', 'world']) will make a string "hello-world" – Gedas Miksenas Sep 15 '22 at 09:50
  • 1
    The parenthesis don't really serve a purpose. They will just be ignored so you can remove them. – SitiSchu Sep 15 '22 at 09:50
  • You can simply convert a list of string into a string by following this code: example = ["This","is","an","example"] " ".join(example) Output: "This is an example" " " identifies that every element of the list will join by a space. – Muhammad Abbas Sep 15 '22 at 09:54

2 Answers2

3

("").join and "".join has no difference at all. Enclosing empty string "" inside parenthesis just makes it an expression and is same as if there was no parenthesis.

For your reference, (1) is same as 1, and parenthesis is mainly used for including some expression, and if you use some IDE like Pycharm/VS Code, it will show you some warning as Redundant Parenthesis for ("")

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0

join concatenates words and you pass the separator, so if you have "abc".join(['foo', 'bar']) then the result is

fooabcbar

The empty string passed as a parameter specifies that nothing will be put in between the elements. The paranthesis is not necessary around the separator.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175