user12150264
user12150264

Reputation:

Type error '<' not supported between instances of str and int

I am working on this leetcode problem : "Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced string s split it in the maximum amount of balanced strings. Return the maximum amount of splitted balanced strings."

Program complies and is error free when I put it in pycharm , it also complies and passes all test cases on Leetcode. Although when I submit it says : "Line 15: TypeError: '<' not supported between instances of 'str' and 'int'" I am confused on whats wrong ?

class Solution:
    def balancedStringSplit(self, s: str) -> int:
        count = 0
        countL = 0
        countR = 0
        i = 1
        while i < len(s):
            if s[i] != s[(i + 1) <= len(s)]:
                count += 1
                i += 2
            else:
                for x in range(len(s[i::])):
                    if s[x] == s[x + 1 < len(s[i::])]:
                        countL += 1
                    if s[x] == s[x + 1] < len(s[i::]): 
                        countR += 1
                    if countR == countL:
                        count += 1
                    else:
                         break
                i +=1
        return count

Upvotes: 0

Views: 783

Answers (1)

oppressionslayer
oppressionslayer

Reputation: 7204

The first syntax you use is correct below, but the second one compares a string to int due to the bracket after 1]. I don't think your running into errors because the else seldom seems to be called.

  if s[x] == s[x + 1 < len(s[i::])]:
                        countL += 1
  if s[x] == s[x + 1] < len(s[i::]):
                        countR += 1

Here's an example where the type error is called due to the bracket after the 1 making it a str to int comparison:

In [207]: ss.balancedStringSplit('12','11111')                                                                                                                
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-207-4754b9274cc7> in <module>
----> 1 ss.balancedStringSplit('12','11111')

<ipython-input-182-c55d872d7f41> in balancedStringSplit(self, s)
     13                     if s[x] == s[x + 1 < len(s[i::])]:
     14                         countL += 1
---> 15                     if s[x] == s[x + 1] < len(s[i::]):
     16                         countR += 1
     17                     if countR == countL:

TypeError: '<' not supported between instances of 'str' and 'int'

Upvotes: 1

Related Questions