Given two non-empty strings s1 and s2, consisting only of lowercase English letters, determine
whether they are anagrams of each other or not.
Two strings are considered anagrams if they contain the same characters with exactly the same
frequencies, regardless of their order.
Examples:
Input: s1 = "geeks" s2 = "kseeg"
Output: true
Explanation: Both the string have same characters with same frequency. So, they are anagrams.
Input: s1 = "allergy", s2 = "allergyy"
Output: false
Explanation: Although the characters are mostly the same, s2 contains an extra 'y' character.
Since the frequency of characters differs, the strings are not anagrams.
Input: s1 = "listen", s2 = "lists"
Output: false
Explanation: The characters in the two strings are not the same — some are missing or extra.
So, they are not anagrams.
Constraints:
1 ≤ s1.size(), s2.size() ≤ 105
s1, s2 consists of lowercase English letters.
Solution:
class Solution
{
public static boolean areAnagrams(String s1, String s2)
{
s1 = s1.replaceAll("\\s", "").toLowerCase();
s2 = s2.replaceAll("\\s", "").toLowerCase();
if (s1.length() != s2.length())
{
return false;
}
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
java.util.Arrays.sort(arr1);
java.util.Arrays.sort(arr2);
return java.util.Arrays.equals(arr1, arr2);
}
}
You are given a string s, and your task is to reverse the string.
Examples:
Input: s = "Geeks"
Output: "skeeG"
Input: s = "for"
Output: "rof"
Input: s = "a"
Output: "a"
Constraints:
1 <= s.size() <= 106
s contains only alphabetic characters (both uppercase and lowercase).
Solution:
class Solution
{
public static String reverseString(String s)
{
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right)
{
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}
You are given a string s. Your task is to determine if the string is a palindrome. A string is
considered a palindrome if it reads the same forwards and backwards.
Examples :
Input: s = "abba"
Output: true
Explanation: "abba" reads the same forwards and backwards, so it is a palindrome.
Input: s = "abc"
Output: false
Explanation: "abc" does not read the same forwards and backwards, so it is not a palindrome.
Constraints:
1 ≤ s.size() ≤ 106
The string s contains only lowercase english letters (a-z).
Solution:
class Solution
{
boolean isPalindrome(String s)
{
s = s.toLowerCase();
int left = 0;
int right = s.length() - 1;
while (left < right)
{
if (s.charAt(left) != s.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}
}
Given a string s, your task is to find the longest palindromic substring within s.
A substring is a contiguous sequence of characters within a string, defined as s[i...j] where 0 ≤ i
≤ j < len(s).
A palindrome is a string that reads the same forward and backward. More formally, s is a
palindrome if reverse(s) == s.
Note: If there are multiple palindromic substrings with the same length, return the first
occurrence of the longest palindromic substring from left to right.
Examples :
Input: s = “forgeeksskeegfor”
Output: “geeksskeeg”
Explanation: There are several possible palindromic substrings like “kssk”, “ss”, “eeksskee”
etc. But the substring “geeksskeeg” is the longest among all.
Input: s = “Geeks”
Output: “ee”
Explanation: "ee" is the longest palindromic substring of "Geeks".
Input: s = “abc”
Output: “a”
Explanation: "a", "b" and "c" are longest palindromic substrings of same length. So, the first
occurrence is returned.
Constraints:
1 ≤ s.size() ≤ 103
s consist of only lowercase English letters.
Solution:
class Solution
{
static String longestPalindrome(String s)
{
int n = s.length();
if (n == 0) return "";
boolean[][] dp = new boolean[n][n];
int start = 0, maxLen = 1;
for (int i = 0; i < n; i++)
{
dp[i][i] = true;
}
for (int i = 0; i < n - 1; i++)
{
if (s.charAt(i) == s.charAt(i + 1))
{
dp[i][i + 1] = true;
start = i;
maxLen = 2;
}
}
for (int len = 3; len <= n; len++)
{
for (int i = 0; i <= n - len; i++)
{
int j = i + len - 1;
if (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1])
{
dp[i][j] = true;
if (len > maxLen)
{
start = i;
maxLen = len;
}
}
}
}
return s.substring(start, start + maxLen);
}
}
You are given two strings s1 and s2. Your task is to find the length of the longest common
substring among the given strings.
Examples:
Input: s1 = "ABCDGH", s2 = "ACDGHR"
Output: 4
Explanation: The longest common substring is "CDGH" with a length of 4.
Input: s1 = "abc", s2 = "acb"
Output: 1
Explanation: The longest common substrings are "a", "b", "c" all having length 1.
Input: s1 = "YZ", s2 = "yz"
Output: 0
Constraints:
1 <= s1.size(), s2.size() <= 103
Both strings may contain upper and lower case alphabets.
Solution:
class Solution
{
public int longestCommonSubstr(String s1, String s2)
{
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n + 1][m + 1];
int maxLen = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (s1.charAt(i - 1) == s2.charAt(j - 1))
{
dp[i][j] = dp[i - 1][j - 1] + 1;
maxLen = Math.max(maxLen, dp[i][j]);
}
else
{
dp[i][j] = 0;
}
}
}
return maxLen;
}
}
Given a string s, return the length of the longest palindromic subsequence.
A subsequence is a sequence that can be derived from the given sequence by deleting some or no
elements without changing the order of the remaining elements.
A palindromic sequence is a sequence that reads the same forward and backward.
Examples:
Input: s = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the longest subsequence which is also a palindrome.
Input: s = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are palindromic and all have a length 1.
Input: s = "agbdba"
Output: 5
Explanation: The longest palindromic subsequence is "abdba", which has a length of 5. The
characters in this subsequence are taken from the original string "agbdba", and they maintain the
order of the string while forming a palindrome.
Constraints:
1 ≤ s.size() ≤ 1000
The string contains only lowercase letters.
Solution:
class Solution
{
public int longestPalinSubseq(String s)
{
int n = s.length();
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++)
{
dp[i][i] = 1;
}
for (int len = 2; len <= n; len++)
{
for (int i = 0; i <= n - len; i++)
{
int j = i + len - 1;
if (s.charAt(i) == s.charAt(j))
{
if (len == 2)
dp[i][j] = 2;
else
dp[i][j] = 2 + dp[i + 1][j - 1];
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][n - 1];
}
}
Given a string s, find the longest repeating non-overlapping substring in it. In other words find 2
identical substrings of maximum length which do not overlap. Return the longest non-
overlapping substring. Return -1 if no such string exists.
Note: Multiple Answers are possible but you have to return the substring whose first
occurrence is earlier.
For Example: "abhihiab", here both "ab" and "hi" are possible answers. But you will have to
return "ab" because its first occurrence appears before the first occurrence of "hi".
Examples:
Input: s = "acdcdacdc"
Output: "acdc"
Explanation: The string "acdc" is the longest Substring of s which is repeating but not
overlapping.
Input: s = "geeksforgeeks"
Output: "geeks"
Explanation: The string "geeks" is the longest subString of s which is repeating but not
overlapping.
Input: s = "heheheh"
Output: "heh"
Explanation: The string "heh" is the longest Substring of s which is repeating but not
overlapping.
Constraints:
1 <= s.size() <= 103
s contains only lowercase English alphabets.
Solution:
class Solution
{
public String longestSubstring(String s)
{
int n = s.length();
if (n <= 1) return "-1";
int[][] dp = new int[n + 1][n + 1];
int maxLen = 0;
int endIndex = 0;
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (s.charAt(i - 1) == s.charAt(j - 1) && dp[i - 1][j - 1] < (j - i))
{
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLen)
{
maxLen = dp[i][j];
endIndex = i;
}
} else {
dp[i][j] = 0;
}
}
}
return maxLen == 0 ? "-1" : s.substring(endIndex - maxLen, endIndex);
}
}
Given a string s consisting of lowercase English Letters. return the first non-repeating character
in s. If there is no non-repeating character, return '$'.
Examples:
Input: s = "geeksforgeeks"
Output: 'f'
Explanation: In the given string, 'f' is the first character in the string which does not repeat.
Input: s = "racecar"
Output: 'e'
Explanation: In the given string, 'e' is the only character in the string which does not repeat.
Input: s = "aabbccc"
Output: -1
Explanation: All the characters in the given string are repeating.
Constraints:
1 ≤ s.size() ≤ 105
Solution:
class Solution
{
public char nonRepeatingChar(String s)
{
int[] freq = new int[256];
for (int i = 0; i < s.length(); i++)
{
freq[s.charAt(i)]++;
}
for (int i = 0; i < s.length(); i++)
{
if (freq[s.charAt(i)] == 1)
{
return s.charAt(i);
}
}
return '$';
}
}
You are given a string s. You have to find the length of the longest substring with all distinct
characters.
Examples:
Input: s = "geeksforgeeks"
Output: 7
Explanation: "eksforg" is the longest substring with all distinct characters.
Input: s = "aaa"
Output: 1
Explanation: "a" is the longest substring with all distinct characters.
Input: s = "abcdefabcbb"
Output: 6
Explanation: The longest substring with all distinct characters is "abcdef", which has a length of
6.
Constraints:
1 ≤ s.size() ≤ 3*104
All the characters are in lowercase.
Solution:
class Solution
{
public int longestUniqueSubstr(String s)
{
int n = s.length();
if (n == 0) return 0;
int[] lastIndex = new int[256];
for (int i = 0; i < 256; i++)
lastIndex[i] = -1;
int maxLen = 0;
int start = 0;
for (int end = 0; end < n; end++)
{
char c = s.charAt(end);
if (lastIndex[c] >= start)
{
start = lastIndex[c] + 1;
}
lastIndex[c] = end;
maxLen = Math.max(maxLen, end - start + 1);
}
return maxLen;
}
}