LeetCode Top Interview Questions — Easy Collection (Strings)

Minal Vaity
3 min readDec 19, 2020

In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string. I have solved five problems on strings and have given out my code and output. There are more ways of solving these problems. This is how I solved mine.

1) Reverse String

Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

String Reverse Java Solution

Input: [“h”,”e”,”l”,”l”,”o”]

Output: [“o”,”l”,”l”,”e”,”h”]

2) Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Reverse Integer Java Solution

Input: x = 123

Output: 321

3) Valid Anagram

Given two strings s and t , write a function to determine if t is an anagram of s.

Valid Anagram Java Solution

Input: s = “listen”, t = “silent”

Output: Is Anagram? True

4) Valid Palindrome

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

Valid Palindrome Java Solution

Input: “A man, a plan, a canal: Panama”

Output: true

5) String to Integer (atoi)

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

String to Integer (atoi) Java Solution

Input: str = “ -42”

Output: -42

So these were my solutions. I hope to come up with more from LeetCode. Hope to see you get motivated as I feel right now!

If you want to have a look at my GitHub account, go visit https://github.com/minu27

And here’s my LinkedIn page https://www.linkedin.com/in/minalvaity/ to connect with me.

Contact me on my email minal.vaity95@gmail.com

--

--