LeetCodeSolutions
LeetCodeSolutions
package com.htc.leetcodesolutions.main;
if (input.length() > 1) {
int start = 0;
int end = 0;
start = i - (length - 1) / 2;
end = i + length / 2;
}
}
return input.substring(start, end + 1);
} else {
return "Input String should contains minimum 2 characters";
}
}
===================================================================================
====
2. Decode String
💡 Problem:
Given an encoded string, return its decoded form.
The encoding rule is: k[encoded_string], where encoded_string inside the brackets
is repeated k times.
You may assume that the input string is always valid.
🔹 Example:
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
🔹 Constraints:
• 1 <= s.length <= 30
• s contains only digits, square brackets, and lowercase English letters.
package com.htc.leetcodesolutions.main;
import java.util.Stack;
return stringBuilder.toString();
}
}
===================================================================================
======
3. Group Anagrams
💡 Problem:
Given an array of strings strs, group anagrams together.
An anagram is a word or phrase formed by rearranging the letters of a different
word.
🔹 Example:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
🔹 Constraints:
• 1 <= strs.length <= 10^4
• strs[i] consists of lowercase English letters.
package com.htc.leetcodesolutions.main;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
}
return new ArrayList<>(anagramMap.values());
}
===================================================================================
============
4. Minimum Window Substring
💡 Problem:
Given two strings s and t, return the minimum window substring of s that contains
all characters of t.
If there is no such substring, return "" (empty string).
🔹 Example:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
🔹 Constraints:
• 1 <= s.length, t.length <= 10^5
• s and t consist of uppercase and lowercase English letters.
package com.htc.leetcodesolutions.main;
import java.util.HashMap;
import java.util.Map;
System.out.println(minWindow(s, t));
}
left++;
}
right++;
}
=========================================================
5. Zigzag Conversion
💡 Problem:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of
rows.
You need to return the converted string when read row-wise.
🔹 Example:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Explanation:
P A H N
A P L S I I G
Y I R
🔹 Constraints:
• 1 <= s.length <= 1000
• 1 <= numRows <= 1000
package com.htc.leetcodesolutions.main;
int index = 0;
boolean down = true;
for (char c : s.toCharArray()) {
rows[index].append(c);
if (index == 0) {
down = true;
} else if (index == numRows - 1) {
down = false;
}
return result.toString();
}
}