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

Let's Develop Together!
Download Telegram
image_2021-11-03_15-57-42.png
26 KB
#N977. Squares of a Sorted Array
problem link=>https://leetcode.com/problems/squares-of-a-sorted-array/

#solution
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans=new int[nums.length];
for(int i=0; i<nums.length; i++){
ans[i]=nums[i]*nums[i];
}

Arrays.sort(ans);
return ans;
}
}
image_2021-11-03_16-02-07.png
28.5 KB
#N852. Peak Index in a Mountain Array
problem link=>https://leetcode.com/problems/peak-index-in-a-mountain-array/

#solution
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int index=-1;
for(int i=0; i<arr.length-1; i++){
if(arr[i]>arr[i+1]){
index=i;
break;
}
}

return index;
}
}
image_2021-11-03_17-41-51.png
27.6 KB
#N1217. Minimum Cost to Move Chips to The Same Position

click➡️ problem link

#solution
class Solution {
public int minCostToMoveChips(int[] position) {
int[] helper=new int[2];

for(int chip:position)
helper[chip%2]++;

return Math.min(helper[0], helper[1]);
}
}
image_2021-11-04_16-42-36.png
75 KB
#N1380. Lucky Numbers in a Matrix

problem link

#solution
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
ArrayList<Integer> ans=new ArrayList<Integer>();
int[] minRow=new int[matrix.length];
int[] maxCol=new int[matrix[0].length];
int min=0, max=0;
for(int i=0; i<minRow.length; i++){
min=matrix[i][0];
for(int j=0; j<matrix[0].length; j++){
min=Math.min(min, matrix[i][j]);
}
minRow[i]=min;
}
for(int i=0; i<maxCol.length; i++){
max=matrix[0][i];
for(int j=0; j<matrix.length; j++){
max=Math.max(max, matrix[j][i]);
}

maxCol[i]=max;
}
for(int i=0; i<maxCol.length; i++){
for(int j=0; j<minRow.length; j++){
if(maxCol[i]==minRow[j])
ans.add(maxCol[i]);
}
}
return ans;
}
}
image_2021-11-04_17-40-36.png
58.8 KB
#N821. Shortest Distance to a Character
problem link

#solution
class Solution {
public int[] shortestToChar(String s, char ch) {
int[] ans=new int[s.length()];
int left=-1, right=-1;

for(int i=0; i<ans.length; i++){
if(s.charAt(i)!=ch){
for(int j=i+1; j<ans.length; j++){
if(s.charAt(j)==ch){
right=j;
break;
}
}
if(left==-1)
ans[i]=right-i;
else if(right==-1)
ans[i]=i-left;
else
ans[i]=Math.min(right-i, i-left);
}
else{
left=i;
right=-1;
}
}

return ans;
}
}
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;
}
}