The following method was written to determine whether its String parameter reads identically left-to-right and right-to-left (the so called palindrome). I am having trouble finding the logic error of this palindrome. I believe the error is that the two conditions in the whole loop can affect checking the characters in the string. Please correct me if I am wrong so I can propose a proper solution.
This may be a stupid question to many of you, but I am new to java programming and this is written question on paper not actual code if that makes sense.
Your logic will only work if the length of input string is an odd number, i.e. 1,3,5 etc.
Because in case the length is even, i will never be equal to j. Example for a string "abba":
while (i == j && S.charAt(i) == S.charAt(j)) { // i = 0, j = 3
i++;
j--;
}
iteration-2:
while (i <= j && S.charAt(i) == S.charAt(j)) { // i = 1 , j = 2
i++;
j--;
}
iteration-3:
while (i <= j && S.charAt(i) == S.charAt(j)) { // i = 2 , j = 1
i++;
j--;
}
This will finally result in StringIndexOutOfBoundsException when i reaches negative value and j reaches a value greater than length of string.
Try below code:
static boolean isPalidrome(String s) {
int i = 0;
int j = s.length() - 1;
while( i <= j && s.charAt(i) == s.charAt(j)) {
i ++;
j--;
}
return i >= j;
}
your code will fail if input string is something like "zz" or "xxxx" meaning even length with same characters so ideally you can try with something like this :
public static boolean isPal(String str) {
int start = 0;
int end = str.length() - 1;
while (start < end) {
final char f = str.charAt(start++);
final char b = str.charAt(end--);
if (f != b) {
return false;
}
}
return true;
}
You may apply following changes in your own code. There are basically two changes which are in your termination condition of while loop i.e. i <= j and return condition of (i >= j)
public static boolean isPalindrome(String S) {
int i = 0, j = S.length() - 1;
while (i <= j && S.charAt(i) == S.charAt(j)) {
i++;
j--;
}
return (i >= j);
}
I'm not sure how to evaluate this line to understand what my code does
while (i < data.length && !result) {
this is my full code.
public static boolean {2,6,-3,7,3} (int[] data, int val) {
boolean result = false;
int i = 0;
while (i < data.length && !result) {
if (data[i] == val) {
result = true;
}
i++;
}
return result;
}
The result variable is used as a way to break out of the while loop before i reaches data.length. It causes the loop to be exited when result becomes true.
It is equivalent to:
boolean result = false;
int i = 0;
while (i < data.length) {
if (data[i] == val) {
result = true;
break; // here we break from the loop explicitly when result becomes true
}
i++;
}
return result;
or even simpler:
int i = 0;
while (i < data.length) {
if (data[i] == val) {
return true; // here we break from the loop using a return statement
// when a match is found
}
i++;
}
return false;
boolean result = false; //initial declaration
int i = 0;
while (i < data.length && !result) {
if (data[i] == val) {
result = true;
// the !result evaluates to false once you've reached this statement
}
So as soon as you find a match of val in the data[]. The flow breaks out of the loop since the condition becomes
any() && false => false
where any() could be either true or false based on your first condition i < data.length.
I am writing a method that goes through a string and returns true if exactly two 'a' characters are found. Obviously, not all conditions are being met, but I can't find how to fix it. The issue states that the boolean type has to be met. However, inside the loop, if a is not found in one iteration, nothing should happen. How do I fix this?
import java.util.*;
import static java.lang.System.out;
public static void main(String[] args) {
}
public static boolean hasTwoA(String s) {
int aCounter = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a') {
aCounter++;
} else if (aCounter == 2) {
return true;
} else if (i == s.length() - 1 && aCounter != 2) {
return false;
}
}
}
}
It would be neater to write it something like:
int aCounter = 0;
for (int i = 0; i < s.length() && aCounter <= 2; ++i) {
if (s.charAt(i) == 'a') ++aCounter;
}
return aCounter == 2;
You can easily do it using Regex, e.g:
String s = "dasfvasaako";
System.out.println(s.matches(".*a.*a.*"));
public static boolean hasTwoA(String s) {
if(s == null || s.length()==1)
return false;
int aCounter = 0;
for (int i = 0; i < s.length()-1; i++) {
if (s.charAt(i) == 'a')
aCounter++;
if (aCounter == 2)
return true;
}
return false;
}
The question is about Solving this problem from codingBat in Java.
Problem Statement:
Given an array of ints, return true if every 2 that appears in the array is next to another 2.
twoTwo({4, 2, 2, 3}) → true
twoTwo({2, 2, 4}) → true
twoTwo({2, 2, 4, 2}) → false
First of all going by the problem statement that every 2 that appears in the array is next to another 2. then
do you think as suggested the outcome for the first inputs shown above
should be true?
twoTwo({4, 2, 2, 3}) → true
Because as I see it it the first 2 itself that appears in the array is next to 4 not 2
am I confused or it's a wrongly stated question? I had to grapple with the problem to somehow get the right code to crack the problem as below but it seems a hotch potch:
public boolean twoTwo(int[] nums) {
if(nums.length==0)
{
return true;
}
if(nums.length==1)
{
return !(nums[0]==2);
}
if((nums.length==2))
{
if((nums[1]==2)&&(nums[0]==2))
return true;
else
return false;
}
for(int i=0;i+2<nums.length;i++)
{
if((nums[i]!=2)&&(nums[i+1]==2)&&(nums[i+2]!=2))
return false;
}
if((nums[nums.length-2]!=2)&&(nums[nums.length-1]==2))
return false;
return true;
}
Any efficient alternate solutions are welcome.
Thanks!
Here's how I would do it. It's a bit easier to follow I think:
public boolean twoTwo(int[] nums)
{
for (int i=0; i<nums.length; i++)
{
if (nums[i] != 2)
continue;
if (i >= 1 && nums[i-1] == 2)
continue;
if (i < (nums.length-1) && nums[i+1] == 2)
continue;
return false;
}
return true;
}
The solution I got to the problem is below:
public boolean twoTwo(int[] nums) {
final int length = nums.length;
for (int i = 0; i < length;){
int count = 0; // Used to count following 2's
while(i < length && nums[i++] == 2){
count++;
}
if(count == 1){ // No adjacent 2's! Set doesn't work.
return false;
}
}
return true; // Didn't come across a lone 2
}
The way that I handle this, is that I count all the adjacent 2's. If the count is not 1, we are good. This means that there was either no 2 at that index, or a group of 2's was present. This holds, since we traverse the array in a single direction.
A good thing about this solution is that it will work for an array of any size. Note that it would have a linear complexity, even though 2 loops are present. They both just traverse using the same index value, only ever sweeping over the array once.
If at any time we find a 2, then check the following only to find there are 0 following 2's (denoted by count), we return false.
public boolean twoTwo(int[] nums) {
for (int i=0; i<nums.length; i++) {
if(nums[i] == 2) { //If current number is 2
if (
// if prev or next is not 2 return true
!(i-1>=0 && nums[i-1] == 2) &&
!(i+1<nums.length && nums[i+1] == 2)
) { return false; }
}
}
return true;
}
For the sake of simplicity and clean code, this code forces the check
i-1>=0 and i+1<nums.length in every iteration.
This can be avoided by iterating from (1...nums.length-1) and checking the edge cases separately.
I know this is an old question, but I came up with a new solution. Short, and with no complicated conditionals.
public boolean twoTwo(int[] nums) {
int position = -2;
boolean result = true;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
result = position == i - 1;
position = i;
}
}
return result;
}
Next to means either before or after. Loop through each number and check the values before and after to see if there's an adjacent 2. The special cases are when you're checking the 1st and last element because there won't be an element before or after to check.
public boolean twoTwo(int[] nums) {
if(nums.length == 1 && nums[0] == 2)
return false;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 2) {
if(i == 0) { // check the next element
if(nums[i+1] != 2)
return false;
}
else if(i == (nums.length - 1)) { // check the previous element
if(nums[i-1] != 2)
return false;
}
else { // check both
if(nums[i+1] != 2 && nums[i-1] != 2)
return false;
}
}
}
return true;
}
Here is mine solution to two two's problem. I think my solution is clear i.e. understandable.
package codingbat.array2;
public class TwoTwo
{
public static void main(String[] args)
{
}
public boolean twoTwo(int[] nums)
{
boolean twoTwo = true;
for (int i = 0; i < nums.length; i++)
{
if (2 == nums[i])
{
if (i > 0 && 2 == nums[i - 1]
|| nums.length > i+1 && 2 == nums[i+1])
{
twoTwo = true;
i++;
}
else
{
twoTwo = false;
break;
}
}
}
return twoTwo;
}
}
public boolean twoTwo(int[] nums) {
for(int i = 0 ; i < nums.length; i++ ) {
int count = 0;
if(nums[i] == 2 ) {
while(i+1 < nums.length && nums[i+1] == 2 ) {
count ++;
i++;
}
if (count == 0 ) {
return false;
}
}
}
return true;
}
public boolean twoTwo(int[] nums) {
for(int i = 0;i<nums.length;i++)
if(nums[i]==2 && !isTwoBeforeOrAfter(nums,i))
return false;
return true;
}
private boolean isTwoBeforeOrAfter(int[] nums,int i){
return i+1<nums.length && nums[i+1]==2 || i-1>=0 && nums[i-1]==2;
}
public boolean twoTwo(int[] nums) {
float two = 0;
double count = 0;
for (int i = 0; i < nums.length; i++) {
if (i < nums.length - 2 && nums[i] == 2 && nums[i + 1] == 2 && nums[i + 2] == 2) {
return true;
}
if (i < nums.length - 1 && nums[i] == 2 && nums[i + 1] == 2) {
count++; //count the pair
}
if (nums[i] == 2) {
two++;
}
}
return ((count * 2) == two);
//each pair contain 2 ,two"s .so pair*2=total two counts
//count
}
public boolean twoTwo(int[] nums) {
boolean two = false;
boolean result = true;
for (int i=0; i<nums.length; i++) {
if (nums[i] == 2) {
if (two) {
result = true;
} else {
result = false;
}
two = true;
} else {
two = false;
}
}
return result;
}
Here's my solution. Enjoy.
public boolean twoTwo(int[] nums)
{
//If the length is 2 or more
if (nums.length >= 2)
{
//If the last char is a 2, but the one before it is not a char, we return false;
if (nums[nums.length - 1] == 2 && nums[nums.length - 2] != 2)
{
return false;
}
//If larger than three, we create a for loop to test if we have any 2s that are alone.
if (nums.length >= 3)
{
for (int i = 1; i < nums.length-1; i++)
{
//If we find a two that is alone, we return false;
if ((nums[i] == 2) && (nums[i-1] != 2 && nums[i+1] != 2))
{
return false;
}
}
}
//If we don't return false, we return true;
return true;
}
//If we have less than two characters, we return true if the length is 0, or \
//One the one number there is not a 2.
else
{
return ((nums.length == 0) || !(nums[0] == 2));
}
}
public boolean twoTwo(int[] nums) {
int len = nums.length;
Boolean check = false;
int count = 0;
for(int i=0; i<len ; i++){
if(nums[i]==2){
count++;
if((i<len-1 && nums[i+1]==2) || (i>0 && nums[i-1]==2)) check = true;
else check = false;
}
}
if(count==0) check = true;
return check;
}
public boolean twoTwo(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++)
if (nums[i] == 2) count++;
else if (count == 1) return false;
else count = 0;
return count != 1;
}
Every time we encounter a 2, we increase the counter of consecutive 2s.
When it's not a 2 — but the counter indicates that there was a single 2 before it —, we know we've found a lonely 2.
Otherwise the search continues, resetting the 2-counter.
easy to understand)
static boolean twoTwo(int[] nums) {
int len = nums.length;
boolean result = true;
boolean found = false;
for(int i=0; i<len; i++){
//if it not 2, no meaning to go true other if-s
if(nums[i] !=2) {found = false; continue;}
// if current element is 2 and found is true(last element is 2)
if(nums[i] ==2 && found) result = true;
// if current element is 2, but last element not
if(nums[i] ==2 && !found){
found = true;
result = false;
}
}
return result;
}
This might be easier to follow if the other suggestions confuse you..
public boolean twoTwo(int[] nums) {
int len = nums.length;
if(len == 0) return true; // no 2's to worry about
if(len == 1) return nums[0] != 2; // make sure it's not a single 2
for(int i = 1; i < len -1; i++){ // loop for each except edge cases
if(nums[i] == 2 && nums[i-1] != 2 && nums[i+1] != 2) return false; // check surrounding
}
if(nums[len - 1] == 2) return nums[len - 2] == 2; //if last num is 2 check for 2 before it
return true; // made it here it's true
}
that one was tough for me... here's mine:
public boolean twoTwo(int[] nums) {
boolean two = false, res = true;
for (int i : nums) {
if (i == 2) {
if (two)
res = true;
else {
two = true;
res = false;
}
} else {
two = false;
}
}
return res;
}
One more alternative. Here is the main idea:
Convert array into String. Add a character different from "2" at the beginning and end of the string, to avoid going out of bounds.
Look for standalone "2" - if element of the string is equal to 2, check whether chars immediately before and after are also equal to "2". If they are it means that not all "2" are adjacent, and therefore method returns false.
public boolean twoTwo(int[] nums) {
// convert array to string
String text = "";
for (int i = 0; i < nums.length; i++) {
text += String.valueOf(nums[i]);
}
text = " " + text + " ";
// find standalone "2"
for (int i = 1; i < text.length() - 1; i++) {
if (text.charAt(i) == '2' && text.charAt(i - 1) != '2' && text.charAt(i + 1)
!= '2') {
return false;
}
}
return true;
}
I'm having trouble with this codingbat exercise:
Returns true if for every * (star) in the string, if there are chars both immediately before and after the star, they are the same.
sameStarChar("xyyzz") // true
sameStarChar("xyzzz") // false
sameStarChar("xaaz") // true
The right answer is:
public boolean sameStarChar(String str) {
for (int i = 1; i < str.length() - 1; i++) {
if (str.charAt(i) == '*' && str.charAt(i - 1) != str.charAt(i + 1)) {
return false;
}
}
return true;
}
What I wrote is:
public boolean sameStarChar(String str) {
for (int i = 1; i < str.length() - 1; i++) {
if (str.substring(i, i + 1) == "*" && str.substring(i - 1, i) != str.substring(i + 1, i + 2)) {
return false;
}
}
return true;
}
I'm having trouble understanding the substring and charAt methods.
Instructions:
Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.
sameStarChar("xy*yzz") → true
sameStarChar("xy*zzz") → false
sameStarChar("*xa*az") → true
Pseudocode:
Loop over every character from 1 to length-1. If the character is an asterisk and the character before it is different from the one after, return false. Otherwise if when you reach the end, return true.
Code:
public boolean sameStarChar(String str){
int len = str.length();
for(int x = 1; x < len-1; x++){
if (str.charAt(x) == '*' && str.charAt(x-1) != str.charAt(x+1)){
return false;
}
}
return true;
}
Comments:
It's a linear time O(n) operation.
str.charAt() is easier to read and understand in this case.
Also, your substring solution should be more like
if (str.substring(i, i + 1).equals("*") &&
!str.substring(i - 1, i).equals(str.substring(i + 1, i + 2))){
//etc.
}
Substring returns a string, and to compare the literal sting value you have to use the .equals() method.
Instead, I count the number of '*' and number of pairs that are matched and check if they both are equal and return true.
public boolean sameStarChar(String str) {
int count=0, noOfPairs=0;
for(int i=1; i<str.length()-1;i++){
if(str.charAt(i)=='*') count++;
if(str.charAt(i)=='*' && str.charAt(i+1)==str.charAt(i-1)) noOfPairs++;
}
if(count==noOfPairs) return true;
return false;
}
Another solution to look at...
public boolean sameStarChar(String str) {
if(str.length()==0) return true;
if(str.length()==1 && str.contains("*")) return true;
if(!str.contains("*")) return true;
int i = 0;
boolean result = false;
while(i<str.length()-1){
if(i!=0 && str.charAt(i) == '*' && str.charAt(i-1) == str.charAt(i+1)){
result = true;
}else if(i!=0 && str.charAt(i) == '*' && str.charAt(i-1) != str.charAt(i+1)){
result = false;
}else if(str.charAt(i) == '*'){
result = true;
}
i++;
}
return result;
}
A version which does not iterate over each individual character in the string by using indexOf(int ch, int fromIndex)
public boolean sameStarChar(String str) {
int i=0;
int l=str.length();
for(i=str.indexOf("*",i);i!=-1;i=str.indexOf("*",i+1))
{
if(i==0 || i==l-1) //saves the program from accessing an out of bounds Index
continue;
else
{
if(str.charAt(i-1)!=str.charAt(i+1));
return false;
}
}
return true;
}