Would like to know the Scala equivalent of Java palindrome function, writing forloop with multiple variables is tricky in scala
class Solution {
public boolean isPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
i++;
}
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
j--;
}
if (i < j && Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j)))
return false;
}
return true;
}
}
Im able to write code in scala for palindrome but the space complexity is O(1) in above solution, and the below one has O(N)
def Ispalindrome(inpt:Option[String]):Boolean ={
inpt match {
case Some(inpt)=> {
val sLetters=inpt.toLowerCase().filter(c=>c.isLetterOrDigit)
(sLetters==sLetters.reverse)
}
case None => false
}
}