'''
30/06/2020

1496. Path Crossing - Easy

Tag: String

Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.

Return True if the path crosses itself at any point, that is, if at any time you are on a location you've previously visited. Return False otherwise.

 

Example 1:



Input: path = "NES"
Output: false 
Explanation: Notice that the path doesn't cross any point more than once.
Example 2:



Input: path = "NESWW"
Output: true
Explanation: Notice that the path visits the origin twice.
 

Constraints:

1 <= path.length <= 10^4
path will only consist of characters in {'N', 'S', 'E', 'W}

'''

from typing import List
# Solution
class Solution1:
    '''
    Time complexity : O(n)
    Space complexity : O(n)
    '''
    def isPathCrossing(self, path: str) -> bool:
        
        x, y = 0, 0
        check = set([(x,y)])
        for p in path:
            if p == 'N':
                y += 1
            elif p == 'S':
                y -= 1
            elif p == 'E':
                x -= 1
            elif p == 'W':
                x += 1
            
            if (x, y) in check:
                return True

            check.add((x, y))
        
        return False


# Unit Test
import unittest
class TestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func("NES"), False)
            self.assertEqual(func("NESWW"), True)

if __name__ == '__main__':
    unittest.main()
f4e2c778976ad0e12fd9a7648b87a35c2a95d736
<mask token>


class Solution1:
    <mask token>
    <mask token>


<mask token>


class TestCase(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func('NES'), False)
            self.assertEqual(func('NESWW'), True)


<mask token>

<mask token>


class Solution1:
    <mask token>

    def isPathCrossing(self, path: str) ->bool:
        x, y = 0, 0
        check = set([(x, y)])
        for p in path:
            if p == 'N':
                y += 1
            elif p == 'S':
                y -= 1
            elif p == 'E':
                x -= 1
            elif p == 'W':
                x += 1
            if (x, y) in check:
                return True
            check.add((x, y))
        return False


<mask token>


class TestCase(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func('NES'), False)
            self.assertEqual(func('NESWW'), True)


<mask token>

<mask token>


class Solution1:
    """
    Time complexity : O(n)
    Space complexity : O(n)
    """

    def isPathCrossing(self, path: str) ->bool:
        x, y = 0, 0
        check = set([(x, y)])
        for p in path:
            if p == 'N':
                y += 1
            elif p == 'S':
                y -= 1
            elif p == 'E':
                x -= 1
            elif p == 'W':
                x += 1
            if (x, y) in check:
                return True
            check.add((x, y))
        return False


<mask token>


class TestCase(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func('NES'), False)
            self.assertEqual(func('NESWW'), True)


<mask token>

<mask token>


class Solution1:
    """
    Time complexity : O(n)
    Space complexity : O(n)
    """

    def isPathCrossing(self, path: str) ->bool:
        x, y = 0, 0
        check = set([(x, y)])
        for p in path:
            if p == 'N':
                y += 1
            elif p == 'S':
                y -= 1
            elif p == 'E':
                x -= 1
            elif p == 'W':
                x += 1
            if (x, y) in check:
                return True
            check.add((x, y))
        return False


<mask token>


class TestCase(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func('NES'), False)
            self.assertEqual(func('NESWW'), True)


if __name__ == '__main__':
    unittest.main()

'''
30/06/2020

1496. Path Crossing - Easy

Tag: String

Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.

Return True if the path crosses itself at any point, that is, if at any time you are on a location you've previously visited. Return False otherwise.

 

Example 1:



Input: path = "NES"
Output: false 
Explanation: Notice that the path doesn't cross any point more than once.
Example 2:



Input: path = "NESWW"
Output: true
Explanation: Notice that the path visits the origin twice.
 

Constraints:

1 <= path.length <= 10^4
path will only consist of characters in {'N', 'S', 'E', 'W}

'''

from typing import List
# Solution
class Solution1:
    '''
    Time complexity : O(n)
    Space complexity : O(n)
    '''
    def isPathCrossing(self, path: str) -> bool:
        
        x, y = 0, 0
        check = set([(x,y)])
        for p in path:
            if p == 'N':
                y += 1
            elif p == 'S':
                y -= 1
            elif p == 'E':
                x -= 1
            elif p == 'W':
                x += 1
            
            if (x, y) in check:
                return True

            check.add((x, y))
        
        return False


# Unit Test
import unittest
class TestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.isPathCrossing
            self.assertEqual(func("NES"), False)
            self.assertEqual(func("NESWW"), True)

if __name__ == '__main__':
    unittest.main()