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

Let's Develop Together!
Download Telegram
#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;
}
}
image_2021-11-09_01-19-45.png
29.1 KB
#N1502. Can Make Arithmetic Progression From Sequence
problem link

#solution
class Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
Arrays.sort(arr);

for(int i=0; i<arr.length-2; i++){
if(arr[i]+arr[i+2]!=2*arr[i+1])
return false;
}

return true;
}
}
image_2021-11-09_01-40-17.png
52.3 KB
#N682. Baseball Game
problem link

#solution
class Solution {
public int calPoints(String[] ops) {
List<Integer> records=new ArrayList<Integer>();
int record=0;

for(int i=0; i<ops.length; i++){
if(ops[i].equals("+"))
records.add(records.get(records.size()-2)+records.get(records.size()-1));
else if(ops[i].equals("C"))
records.remove(records.size()-1);
else if(ops[i].equals("D"))
records.add(2*records.get(records.size()-1));
else
records.add(Integer.parseInt(ops[i]));
}

for(int i:records)
record+=i;

return record;
}
}
image_2021-11-09_01-58-29.png
50.4 KB
#N496. Next Greater Element I
problem link

#solution
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] res=new int[nums1.length];
boolean isFound;

for(int i=0; i<nums1.length; i++){
isFound=false;
for(int j=0; j<nums2.length; j++){
if(nums2[j]==nums1[i])
isFound=true;

if(isFound){
if(nums2[j]>nums1[i]){
res[i]=nums2[j];
break;
}
else if(j==nums2.length-1)
res[i]=-1;
}
}
}

return res;
}
}
image_2021-11-09_14-51-47.png
50 KB
#medium

#N1433. Check If a String Can Break Another String
problem link

#solution
class Solution {
public boolean checkIfCanBreak(String s1, String s2) {
char arr1[]=s1.toCharArray();
char arr2[]=s2.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);

int count1=0, count2=0, l=s1.length();

for(int i=0; i<l; i++){
if(arr1[i]>=arr2[i])
count1++;
if(arr2[i]>=arr1[i])
count2++;
}

return count1==l||count2==l;
}
}
image_2021-11-09_15-12-58.png
52.4 KB
#medium

#N791. Custom Sort String
problem link

#solution
class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
char[] arr1=order.toCharArray();
char[] arr2=s.toCharArray();

for(int i=0; i<order.length(); i++){
for(int j=0; j<s.length(); j++){
if(arr1[i]==arr2[j]){
res.append(""+arr1[i]);
arr2[j]=' ';
}
}
}

for(int i=0; i<arr2.length; i++){
if(arr2[i]!=' ')
res.append(""+arr2[i]);
}

return res.toString();
}
}

p.s. runtime is O(m•n). the worst ever
Leetcode in Java && Oracle
image_2021-11-09_15-12-58.png
image_2021-11-09_15-29-43.png
57.8 KB
#updated

class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
int[] arr=new int[26];

for(int i=0; i<s.length(); i++){
arr[s.charAt(i)-'a']++;
}

for(int i=0; i<order.length(); i++){
while(arr[order.charAt(i)-'a']>0){
res.append(order.charAt(i));
arr[order.charAt(i)-'a']--;
}
}

for(int i=0; i<26; i++){
while(arr[i]>0){
res.append((char) (i+'a'));
arr[i]--;
}
}

return res.toString();
}
}

p.s.: better runtime than previous one)
image_2021-11-09_18-00-09.png
60.1 KB
#N1122. Relative Sort Array
problem link

#solution
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count=new int[1001];
int[] res=new int[arr1.length];

for(int i=0; i<arr1.length; i++){
count[arr1[i]]++;
}
int index=0;
for(int i=0; i<arr2.length; i++){
while(count[arr2[i]] > 0){
res[index]=arr2[i];
index++;
count[arr2[i]]--;
}
}

for(int i=0; i<count.length; i++){
if(count[i]!=0){
while(count[i]>0){
res[index]=i;
index++;
count[i]--;
}
}
}

return res;
}
}
image_2021-11-10_13-52-56.png
19.9 KB
#N441. Arranging Coins
problem link

#solution
class Solution {
public int arrangeCoins(int n) {
return ((int)(Math.pow((1+8*(long)n), 0.5))-1)/2;
}
}

p.s. Yeah, math really helps to solve this kind of problems😅
image_2021-11-11_21-24-38.png
27.7 KB
#N1413. Minimum Value to Get Positive Step by Step Sum
problem link

#solution
class Solution {
public int minStartValue(int[] nums) {
int sum=0, minValue=0;

for(int num:nums){
sum+=num;
minValue=Math.min(minValue, sum);
}

return 1-minValue;
}
}