# Swift solution

• ``````class Solution {
func solveEquation(_ equation: String) -> String {
let chars = Array(equation.characters)
var numChars = [Character]()
var variableSum = 0
var xSum = 0
var op = "+"
var sign = 1
var i = 0

while i < chars.count {
let char = chars[i]
if char == "x" {
if numChars.isEmpty {
numChars.append("1")
}
if op == "+" {
xSum += Int(String(numChars))! * sign
} else if op == "-" {
xSum -= Int(String(numChars))! * sign
}
numChars.removeAll()
} else if char == "+" || char == "-" ||  char == "=" {
if numChars.count > 0 {
if op == "+" {
variableSum += Int(String(numChars))! * sign
} else if op == "-" {
variableSum -= Int(String(numChars))! * sign
}
numChars.removeAll()
}
if char == "+" {
op = "+"
} else if char == "-" {
op = "-"
} else if char == "=" {
sign = -1
op = "+"
}
} else {
numChars.append(char)
}
i += 1
}
if numChars.count > 0 {
if op == "+" {
variableSum += Int(String(numChars))! * sign
} else if op == "-" {
variableSum -= Int(String(numChars))! * sign
}
}

if xSum == 0 && variableSum == 0 {
return "Infinite solutions"
}
if xSum == 0 && variableSum != 0 {
return "No solution"
}

return "x=\(-variableSum / xSum)"
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.