Concept of Frequency Array

Frequency array is an array which stores the value of occurence of each character.

Concept of Frequency Array

Frequency array is an array which stores the value of occurence of each element in the array.

Let us take examples : 

string : aabbddd

        0         0          0           0           0

a

e

d

c

b

Concept of Frequency Array

 aabbddd

        1         0          0           0           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         0          0           0           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         1          0           0           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         2          0           0           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         2          0           1           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         2          0           2           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

 aabbddd

        2         2          0           3           0

a

e

d

c

b

Traversing the array

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

The size of the frquency array will be 26. As there are 26 alphabets.

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

How to form this array ?

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

We need to think in such a way that the frequency of characters must be stored at the index related to elements

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

We need to think in such a way that the frequency of characters must be stored at the index related to elements

a

0

b

1

c

2

d

3

and so on !!!!

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

But how to convert alphabets to numbers for indexing in array?

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

ASCII values

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

ASCII values

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

97 - 97 = 0

98 - 97 = 1

99 - 97 = 2

100 - 97 = 3

101 - 97 = 4

a

d

c

b

e

Concept of Frequency Array

        2         2          0           3           0

a

e

d

c

b

0

4

3

2

1

Index of any element 

=

ASCII value of element - ASCII value of 'a'

index

'b' - 'a'

1

Approach towards the problem

We have learnt the concept of frequency array!!!

Approach towards the problem

We have learnt the concept of frequency array!!!

Now we only need to do what the question demands..!!

Approach towards the problem

We have learnt the concept of frequency array!!!

1. Make two frequency arrays for the left half and the right half.

Approach towards the problem

We have learnt the concept of frequency array!!!

1. Make two frequency arrays for the left half and the right half.

2. Traverse the first half and store it in first frequency array and then traverse the second half and store it in the next frequency array.

Approach towards the problem

We have learnt the concept of frequency array!!!

1. Make two frequency arrays for the left half and the right half.

2. Traverse the first half and store it in first frequency array and then traverse the second half and store it in the next frequency array.

   3. Traverse both the arrays and check whether all the elements have same frequency or not.

Approach towards the problem

We have learnt the concept of frequency array!!!

If yes then its a Lapindrome then output

"YES"

otherwise

"NO"

 

Approach towards the problem

string  = "abccab"

Approach towards the problem

string  = "abccab"

left

abc

               1               1             1

0

2

1

Approach towards the problem

string  = "abccab"

left

abc

               1               1             1

0

2

1

right

cab

               1               1             1

0

2

1

Approach towards the problem

Lets move towards the Coding part of the problem!!

Done with the understannding of Concept..!!

Approach towards the problem

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		string s;
		cin >> s;
		int left[26] = {0};
		int right[26] = {0};
		int len = s.length();
        for(int  i = 0; i<len/2; i++)
		{
			int index = s[i] - 'a';
			left[index]++;
		}
        //we are using (len+1)/2 becuase if the array is of odd length 
        //then it will skip the middle element.
        // 5 -> left(0,1) right (3,4)
        //6  -> (0,1,2)  right(3,4,5)
		for(int  i = (len+1)/2; i<len; i++)
		{
			int index = s[i] - 'a';
			right[index]++;
		}
        int case = 0;
		for(int i = 0; i<26; i++)
		{
			if(left[i] != right[i])
				case = 1;
		}
		if(case == 0)
			cout <<"YES" << endl;
		else
			cout << "NO" << endl;
	}
    return 0;
}

deck

By Chirayu Jain