Leetcode in Java && Oracle
420 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-11-04_17-50-48.png
40.4 KB
#N922. Sort Array By Parity II
problem link

#solution
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int[] ans=new int[nums.length];
int evenIndex=0, oddIndex=1;

for(int i=0; i<nums.length; i++){
if(nums[i]%2==0){
ans[evenIndex]=nums[i];
evenIndex+=2;
}
else{
ans[oddIndex]=nums[i];
oddIndex+=2;
}
}

return ans;
}
}
image_2021-11-05_17-43-17.png
33.9 KB
#N1441. Build an Array With Stack Operations

problem link

#solution
class Solution {
public List<String> buildArray(int[] target, int n) {
ArrayList<String> stack=new ArrayList<String>();
int x=0;
for(int i=1;i<=n && x<target.length; i++){
stack.add("Push");
if(target[x]==i) x++;
else stack.add("Pop");
}

return stack;
}
}
image_2021-11-07_00-54-43.png
27.2 KB
#N1108. Defanging an IP Address
problem link

#solution
class Solution {
public String defangIPaddr(String address) {
String res="";
for(char ch:address.toCharArray()){
if(ch=='.')
res+="[.]";
else
res+=ch;
}

return res;
}
}
image_2021-11-07_01-01-04.png
27.2 KB
#N771. Jewels and Stones
problem link

#solution
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int count=0;
for(char j:jewels.toCharArray()){
for(char s:stones.toCharArray()){
if(j==s) count++;
}
}

return count;
}
}
image_2021-11-07_03-38-55.png
42.6 KB
#N1678. Goal Parser Interpretation
problem link

#solution
class Solution {
public String interpret(String command) {
String res="";
for(int i=0; i<command.length()-1; i++){
if(command.charAt(i)=='('&&command.charAt(i+1)==')')
res+="o";
else if(command.charAt(i)=='('&&command.charAt(i+1)=='a')
res+="al";
else if(command.charAt(i)=='G')
res+="G";
}
if(command.charAt(command.length()-1)=='G') res+="G";

return res;
}
}
image_2021-11-07_04-12-12.png
31.4 KB
#N1221. Split a String in Balanced Strings
problem link

#solution
class Solution {
public int balancedStringSplit(String s) {
int count=0, balance=0;
for(char ch:s.toCharArray()){
if(ch=='R')
balance--;
if(ch=='L')
balance++;
if(balance==0)
count++;
}

return count;
}
}
image_2021-11-07_04-26-24.png
44 KB
#N1859. Sorting the Sentence
problem link

#solution
class Solution {
public String sortSentence(String s) {
String[] words=s.split(" ");
String[] sorted=new String[words.length];
String res="";

for(String word:words){
sorted[Character.getNumericValue(word.charAt(word.length()-1))-1]=word.substring(0, word.length()-1);
}

for(int i=0; i<sorted.length; i++){
res+=sorted[i];
if(i!=sorted.length-1)
res+=" ";
}

return res;
}
}
image_2021-11-07_04-36-36.png
30.3 KB
#N1614. Maximum Nesting Depth of the Parentheses
problem link

#solution
class Solution {
public int maxDepth(String s) {
int count=0, depth=0;
for(char ch:s.toCharArray()){
if(ch=='(')
count++;
if(ch==')'){
depth=Math.max(depth, count--);
}
}

return depth;
}
}
image_2021-11-07_04-41-37.png
32.4 KB
#N1832. Check if the Sentence Is Pangram
problem link

#solution
class Solution {
public boolean checkIfPangram(String sentence) {
int[] alphabet=new int[26];

for(char ch:sentence.toCharArray()){
alphabet[ch-'a']++;
}

for(int num:alphabet)
if(num==0)
return false;

return true;
}
}
#statistics
Till this time, 91 in total,
86 easy;
5 medium;
type of problem solutions have been posted
image_2021-11-07_16-32-18.png
27.9 KB
#N709. To Lower Case
problem link

#solution
class Solution {
public String toLowerCase(String s) {
char[] res=s.toCharArray();

for(int i=0; i<res.length; i++){
if(res[i]>='A'&&res[i]<='Z')
res[i]=(char)(res[i]+32);
}

return String.valueOf(res);
}
}
or another option, using the String method toLowerCase(), just write return s.toLowerCase(); .
image_2021-11-07_17-31-04.png
28.4 KB
#N1844. Replace All Digits with Characters
problem link

#solution
class Solution {
public String replaceDigits(String s) {
char[] ch=s.toCharArray();

for(int i=1; i<ch.length; i=i+2){
ch[i]=(char)(ch[i-1]+Character.getNumericValue(ch[i]));
}

return String.valueOf(ch);
}
}
image_2021-11-07_18-44-51.png
45.9 KB
#N2000. Reverse Prefix of Word
problem link

#solution
class Solution {
public String reversePrefix(String word, char ch) {
char[] letters=word.toCharArray();
int index=-1;
char temp;
for(int i=0; i<letters.length; i++){
if(letters[i]==ch){
index=i;
for(int j=0; j<(index+1)/2; j++){
temp=letters[j];
letters[j]=letters[index-j];
letters[index-j]=temp;
}
break;
}
}

return String.valueOf(letters);
}
}
image_2021-11-07_18-54-02.png
57.8 KB
#N1704. Determine if String Halves Are Alike
problem link

#solution
class Solution {
char[] vowels={'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
public boolean halvesAreAlike(String s) {
String half1=s.substring(0, s.length()/2);
String half2=s.substring(s.length()/2);
int count=0;

for(int i=0; i<half1.length(); i++){
if(isVowel(half1.charAt(i)))
count++;
if(isVowel(half2.charAt(i)))
count--;
}

return count==0;
}

boolean isVowel(char ch){
for(char vowel:vowels){
if(ch==vowel)
return true;
}
return false;
}
}
image_2021-11-07_19-05-02.png
28.9 KB
#N1967. Number of Strings That Appear as Substrings in Word
problem link

#solution
class Solution {
public int numOfStrings(String[] patterns, String word) {
int count=0;
for(String pattern:patterns){
if(word.contains(pattern))
count++;
}

return count;
}
}
image_2021-11-07_20-19-50.png
40.6 KB
#N1309. Decrypt String from Alphabet to Integer Mapping
problem link

#solution
class Solution {
public String freqAlphabets(String s) {
StringBuilder res=new StringBuilder();
for(int i=0; i<s.length(); i++){
if(i<s.length()-2&&s.charAt(i+2)=='#'){
res.append((char)(Integer.parseInt(s.substring(i, i+2))+96));
i=i+2;
}
else
res.append((char)(Character.getNumericValue(s.charAt(i))+96));
}

return res.toString();
}
}
Leetcode in Java && Oracle
image_2021-11-07_20-19-50.png
#useful
Till this time, we used only Strings to add Strings each other, but we got the point that this is not efficient at all. So then, we will use the StringBuilder class as they are mutable unlike String. And they are more efficient in adding and reversing Strings.
For more info: https://docs.oracle.com/javase/9/docs/api/java/lang/StringBuilder.html
image_2021-11-08_01-43-03.png
46.9 KB
#medium

#N1769. Minimum Number of Operations to Move All Balls to Each Box
problem link

#solution
class Solution {
public int[] minOperations(String boxes) {
char[] ch=boxes.toCharArray();
int[] answer=new int[boxes.length()];
int count;

for(int i=0; i<answer.length; i++){
count=0;
for(int j=0; j<answer.length; j++)
if(ch[j]=='1')
count+=Math.abs(i-j);

answer[i]=count;
}

return answer;
}
}
image_2021-11-08_01-52-44.png
26.5 KB
#medium

#N1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
problem link

#solution
class Solution {
public int minPartitions(String n) {
int max=0;

for(char num:n.toCharArray())
max=Math.max(max, num-'0');

return max;
}
}
image_2021-11-08_18-21-25.png
32.7 KB
#N944. Delete Columns to Make Sorted
problem link

#solution
class Solution {
public int minDeletionSize(String[] strs) {
int count=0;
for(int i=0; i<strs[0].length(); i++){
for(int j=0; j<strs.length-1; j++){
if(strs[j].charAt(i)>strs[j+1].charAt(i)){
count++;
break;
}
}
}

return count;
}
}