1

I am new to java and practicing some programs.Can someone please explain me the following program at //line 10

public static void main(String[] args) {
        String str1 = "xxyz";
        String str2 = "yxzx";
        System.out.println("Original strings: " + str1 + "  " + str2);
        System.out.println(stringPermutation(str1, str2));
    }

    public static void stringPermutation(String str1, String str2) {
        int[] arr = new int[500];
        for (int i = 0; i < str1.length(); i++) {
     System.out.println(arr[(int) str1.charAt(i)] += 1); //line 10

        } 
}

it's displaying the below output :

Original strings: xxyz  yxzx
1
2
1
1

Am trying to understand, how arr[(int) str1.charAt(i)]i.e, arr['x'] is possible. Please help me understanding this.

2
  • stackoverflow.com/questions/48685135/… Commented Feb 27, 2020 at 12:23
  • 2
    Characters are basicly int values, for example x==120. You can check some ASCII table to see values of all the characters. Commented Feb 27, 2020 at 12:23

3 Answers 3

2

When you initialise an empty integer array the initial value for each of the item is zero, there for each index of arr will contain 0

int[] arr = new int[500];
arr[0] = 0;
arr[1] = 0;
//...
arr[500] = 0;

The ASCII value for x is 120, y is 121 and z is 122, since the arr field contains 500 item then 120, 121 and 122 is in range.

In your loop you are adding 1 to each of the element. therefore in your str1 = "xxyz" when the first x is encounter 1 is added to index arr[120] so arr[120] becomes 1 and when x is encounter again 1 is added to the value which makes arr[120] becomes 2.

arr[(int) 'x'] += 1 //=> arr[120] + 1 = (0 + 1) = 1
arr[(int) 'x'] += 1 //=> arr[120] + 1 = (1 + 1) = 2
arr[(int) 'y'] += 1 //=> arr[121] + 1 = (0 + 1) = 1
arr[(int) 'z'] += 1 //=> arr[122] + 1 = (0 + 1) = 1

arr['x'] is possible because in java char data type is a single 16-bit integer and int is 32-bit signed integer.

Update:

In the continuation for the program the second loop on str2 is deducting one at the char index, if str2 is a permutation of str1 the value all the item in arr should reset to 0.

After the loop on str1. The values in the array are

arr[0] = 0
//...
arr[120] = 2 //arr['x']
arr[121] = 1 //arr['y']
arr[122] = 1 //arr['z']
//...
arr[500] = 0

When str2 = "yxzx"

arr[(int) 'y'] -= 1 //=> arr[121] - 1 = (1 - 1) = 0
arr[(int) 'x'] -= 1 //=> arr[120] - 1 = (2 - 1) = 1
arr[(int) 'z'] -= 1 //=> arr[122] - 1 = (1 - 1) = 0
arr[(int) 'x'] -= 1 //=> arr[120] - 1 = (1 + 1) = 0

After the loop on str2 the values will be reset to 0

arr[0] = 0
//...
arr[120] = 0 //arr['x']
arr[121] = 0 //arr['y']
arr[122] = 0 //arr['z']
//...
arr[500] = 0

Hence looping through all the array if all the values are zeros then str2 is a permutation of str1.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the detailed explanation.
In Continuation to this program to 'Check if a given string is a permutation of another specified string' , am unable to understand the below logic for (int i = 0; i < str2.length(); i++) { System.out.println(arr[(int) str2.charAt(i)] -= 1); } for (int i = 0; i < arr.length; i++) { if (arr[i] != 0) return false; } return true; }
can you please explain me how for (int i = 0; i < arr.length; i++) { if (arr[i] != 0) return false; } is used to check if string str2 is a perumtation of string str1.
1

Because of ASCII value of x is 120, y is 121 and z is 122. So, Now

arr[x] => arr[120] = 1
arr[x] => arr[120] = 2
arr[y] => arr[121] = 1
arr[z] => arr[122] = 1

2 Comments

Thank you.Though some people downvoted it, i understood the concept now.
Thank you. Never thought that char will be converted to ASCII integer.
1

Yes, you can. A char variable can hold any integer from 0 to 65,535. The value of a char literal is equal to an integer from the ASCII table.

public class Main {
    public static void main(String[] args) {
        char[] charArr = { '0', 'A', 'z' };
        int[] anArr = new int[123];

        // Initialize
        for (int i = 1; i < 123; i++) {
            anArr[i] = i * 10;
        }

        // Display
        for (char c : charArr) {
            System.out.print(anArr[c] + "\t");// Will display 480   650 1220
        }
    }
}

Explanation:

anArr['0'] = anArr[48] which has 48*10 stored in it
anArr['A'] = anArr[65] which has 65*10 stored in it
anArr['z'] = anArr[122] which has 122*10 stored in it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.