Write a method to compute if a string is a palindrome, disregarding spaces.
Anonymous
In Java: private static boolean isPalindrome(String str) { int leftIndex = 0; int rightIndex = str.length() - 1; while(leftIndex < rightIndex) { if (str.charAt(leftIndex) == ' ') { leftIndex++; continue; } if (str.charAt(rightIndex) == ' ') { rightIndex--; continue; } if (str.charAt(leftIndex) != str.charAt(rightIndex)) { return false; } leftIndex++; rightIndex--; } return true; } N/2 iterations for characters + N iterations for whitespaces = O(N) complexity
Check out your Company Bowl for anonymous work chats.