InterviewBit-Day9

Practice

Palindrome String

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Example:

"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome.

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Solution {
public int isPalindrome(String A) {
String start, end;
for (int i = 0, j = A.length() - 1; i < j; ) {
if ((A.charAt(i) >= 'a' && A.charAt(i) <= 'z') ||
(A.charAt(i) >= 'A' && A.charAt(i) <= 'Z') ||
(A.charAt(i) >= '0' && A.charAt(i) <= '9')) {
start = String.valueOf(A.charAt(i));
i++;
} else {
i++;
continue;
}
if ((A.charAt(j) >= 'a' && A.charAt(j) <= 'z') ||
(A.charAt(j) >= 'A' && A.charAt(j) <= 'Z') ||
(A.charAt(j) >= '0' && A.charAt(j) <= '9')) {
end = String.valueOf(A.charAt(j));
j--;
} else {
j--;
i--;
continue;
}
if (start.length() != 0 && end.length() != 0) {
if (!start.equalsIgnoreCase(end)) {
return 0;
}
}
}

return 1;
}
}

Amazing Subarrays

You are given a string S, and you have to find all the amazing substrings of S.

Amazing Substring is one that starts with a vowel (a, e, i, o, u, A, E, I, O, U).

Input

1
Only argument given is string S.

Output

1
Return a single integer X mod 10003, here X is number of Amazing Substrings in given string.

Constraints

1
2
1 <= length(S) <= 1e6
S can have special characters

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input
ABEC

Output
6

Explanation
Amazing substrings of given string are :
1. A
2. AB
3. ABE
4. ABEC
5. E
6. EC
here number of substrings are 6 and 6 % 10003 = 6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
public int solve(String A) {
int len = A.length();
if (len == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < len; i++) {
if (A.charAt(i) == 'a' || A.charAt(i) == 'e' ||
A.charAt(i) == 'i' || A.charAt(i) == 'o' ||
A.charAt(i) == 'u' || A.charAt(i) == 'A' ||
A.charAt(i) == 'E' || A.charAt(i) == 'I' ||
A.charAt(i) == 'O' || A.charAt(i) == 'U') {
count = count + len - i;
}
count = count % 10003;
}

return count;
}
}

Minimum Characters required to make a String Palindromic

You are given a string. The only operation allowed is to insert characters in the beginning of the string. How many minimum characters are needed to be inserted to make the string a palindrome string

Example:
Input: ABC
Output: 2
Input: AACECAAAA
Output: 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public int solve(String A) {
String B = new StringBuilder(A).reverse().toString();
int count = 0;
for (int i = 0, j = 0; i < A.length() && j < B.length();) {
if (B.charAt(j) == A.charAt(i)) {
i++;
j++;
} else {
count++;
i = 0;
j = count;
}
}
return count;
}
}
0%