You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 23, 2025. It is now read-only.
Given a string s, find the length of the longest substring without repeating characters
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
publicclassSolution {
staticpublicintlengthOfLongestSubstring(Strings) {
intresult = 0;
Set<Character> charSet = newHashSet();
intwindowStart = 0;
for (intwindowEnd = 0; windowEnd < s.length(); windowEnd++) {
// if the character at right pointer is duplicate, keep removing// character at left pointer until the duplicate character is removed.while (charSet.contains(s.charAt(windowEnd))) {
charSet.remove(s.charAt(windowStart));
windowStart++;
}
// add the character at the right pointer to the setcharSet.add(s.charAt(windowEnd));
// check if the current substring length is maximumresult = Math.max(result, windowEnd - windowStart + 1);
}
returnresult;
}
publicstaticvoidmain(String[] args) {
System.out.println(lengthOfLongestSubstring("abcadef"));
}
}
Reverse String
Write a function that reverses a string.
The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Given a string s, reverse the order of characters in each word
within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
classSolution {
publicStringreverseWords(Strings) {
Stringans = "";
Stringtemp = "";
for (inti = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
ans += temp;
ans += " ";
temp = "";
} else {
temp = s.charAt(i) + temp;
}
}
ans += temp;
returnans;
}
}
Valid Anagram
Given two strings s and t, return true if t is an anagram of s,
and false otherwise.
An Anagram is a word or phrase formed by rearranging the
letters of a different word or phrase, typically using all
the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Given two strings ransomNote and magazine,
return true if ransomNote can be constructed from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
classSolution {
publicbooleancanConstruct(StringransomNote, Stringmagazine) {
if (ransomNote.length() > magazine.length())
returnfalse;
int[] alpha = newint[26];
for (charele : magazine.toCharArray()) {
alpha[ele - 97]++;
}
for (charele : ransomNote.toCharArray()) {
if (alpha[ele - 97]-- <= 0)
returnfalse;
}
returntrue;
}
}
First Unique Character in a String
Given a string s, find the first non-repeating character in it
and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Given a string s, find the length of the longest substring without repeating characters
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
publicclassSolution {
staticpublicintlengthOfLongestSubstring(Strings) {
intresult = 0;
Set<Character> charSet = newHashSet();
intwindowStart = 0;
for (intwindowEnd = 0; windowEnd < s.length(); windowEnd++) {
// if the character at right pointer is duplicate, keep removing// character at left pointer until the duplicate character is removed.while (charSet.contains(s.charAt(windowEnd))) {
charSet.remove(s.charAt(windowStart));
windowStart++;
}
// add the character at the right pointer to the setcharSet.add(s.charAt(windowEnd));
// check if the current substring length is maximumresult = Math.max(result, windowEnd - windowStart + 1);
}
returnresult;
}
publicstaticvoidmain(String[] args) {
System.out.println(lengthOfLongestSubstring("abcadef"));
}
}
Print frequencies of characters of string of lower case alphabets
classSolution {
publicstaticvoidmain(String[] args) {
Stringstr = "geeksforgeeks";
int[] count = newint[26];
for (inti = 0; i < str.length(); i++) {
count[str.charAt(i) - 'a']++;
}
for (inti = 0; i < 26; i++) {
if (count[i] > 0) {
System.out.println((char) (i + 'a') + " " + count[i]);
}
}
}
}