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

Let's Develop Together!
Download Telegram
image_2021-12-13_01-05-22.png
34.9 KB
#N1974. Minimum Time to Type Word Using Special Typewriter
problem link

#solution
class Solution {
public int minTimeToType(String word) {
int time = 0;
char temp = 'a';

for(char ch: word.toCharArray()){
int time1 = Math.abs(ch-temp);
time = time + Math.min(time1, 26-time1) + 1;

temp = ch;
}

return time;
}
}
image_2021-12-13_09-07-24.png
41.2 KB
#N1446. Consecutive Characters
problem link

#solution
class Solution {
public int maxPower(String s) {
if(s.length() == 1) return 1;
int count = 0, power = 0;
char temp = s.charAt(0);

for(char ch: s.toCharArray()){
if(temp == ch) count++;
else{
count = 1;
temp = ch;
}
power = Math.max(power, count);
}

return power;
}
}
image_2021-12-13_11-22-23.png
50.7 KB
#N2085. Count Common Words With One Occurrence
problem link

#solution
class Solution {
public int countWords(String[] words1, String[] words2) {
int count=0;

Map<String, Integer> map1 = new HashMap<>();
Map<String, Integer> map2 = new HashMap<>();
for(String word: words1)
map1.put(word, map1.getOrDefault(word, 0) +1);

for(String word: words2)
map2.put(word, map2.getOrDefault(word, 0) +1);

for (String word: words1)
if(map1.get(word) == 1 && map2.getOrDefault(word, 0) == 1) count++;

return count;
}
}
image_2021-12-13_11-51-54.png
35 KB
#N2042. Check if Numbers Are Ascending in a Sentence
problem link

#solution
class Solution {
public boolean areNumbersAscending(String s) {
int temp = 0;
for(String word: s.split(" ")){
if(Character.isDigit(word.charAt(0))){
int num = Integer.parseInt(word);
if(num > temp) temp = num;
else return false;
}
}

return true;

}
}
image_2021-12-15_23-25-48.png
52.9 KB
#N824. Goat Latin
problem link

#solution
class Solution {
public String toGoatLatin(String sentence) {
StringBuilder sb = new StringBuilder();
int count=1;
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
for(String word: sentence.split(" ")){
if(vowels.contains(word.charAt(0)))
sb.append(word).append("ma");
else
sb.append(word.substring(1)).append(word.charAt(0)).append("ma");

for(int i=1; i<=count; i++)
sb.append("a");

count++;
sb.append(" ");
}

return sb.toString().trim();
}
}
image_2021-12-16_23-04-15.png
29.7 KB

#N1876. Substrings of Size Three with Distinct Characters
problem link

#solution
class Solution {
public int countGoodSubstrings(String s) {
int count = 0;

for(int i=1; i<s.length()-1; i++)
if(s.charAt(i-1) != s.charAt(i) && s.charAt(i+1) != s.charAt(i) && s.charAt(i+1) != s.charAt(i-1))
count++;


return count;
}
}
image_2021-12-18_15-49-06.png
24.3 KB

#N1332. Remove Palindromic Subsequences
problem link

#solution
class Solution {
public int removePalindromeSub(String s) {
if (s.equals(new StringBuilder(s).reverse().toString())) return 1;

return 2;
}
}
image_2021-12-18_16-31-44.png
38.2 KB
#N2068. Check Whether Two Strings are Almost Equivalent
problem link

#solution
class Solution {
public boolean checkAlmostEquivalent(String word1, String word2) {
int count[] = new int[26];

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

for(int i=0; i<26; i++)
if(Math.abs(count[i]) > 3) return false;

return true;
}
}
image_2021-12-22_03-19-23.png
23.8 KB
#N231. Power of Two
problem link

#solution
class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1) return false;
if(n==1) return true;

if(n%2==0) return isPowerOfTwo(n/2);
return false;
}
}
👍2
Update your telegram😁
👍71
image_2022-01-01_05-27-59.png
21.6 KB
#N2119. A Number After a Double Reversal
problem link

#solution
class Solution {
public boolean isSameAfterReversals(int num) {
return num==0||!(num%10==0);
}
}
image_2022-01-03_14-36-34.png
43 KB
#N2108. Find First Palindromic String in the Array
problem link

#solution
class Solution {
public String firstPalindrome(String[] words) {

for(String word: words){
if(check(word))
return word;
}

return "";
}

public boolean check(String word){
int l = word.length();
for(int i=0; i<l; i++){
if(word.charAt(i) != word.charAt(l-i-1))
return false;
}

return true;
}
}
image_2022-01-04_16-19-41.png
29.5 KB
#N1009. Complement of Base 10 Integer
problem link

#solution
class Solution {
public int bitwiseComplement(int n) {
if(n==0) return 1;
int power = 0;
for(int i=n; i>0; i/=2)
power++;

int temp=1;
while(power-- >0)
temp*=2;

return temp-n-1;
}
}
image_2022-01-05_01-14-41.png
34.9 KB
#N2124. Check if All A's Appears Before All B's
problem link

#solution
class Solution {
public boolean checkString(String s) {
boolean isFound = false;

for(int i=0; i<s.length(); i++){
if(s.charAt(i) == 'b')
isFound = true;

if(isFound && s.charAt(i) == 'a')
return false;
}

return true;
}
}
image_2022-01-05_01-37-27.png
56.5 KB
#N884. Uncommon Words from Two Sentences
problem link

#solution
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> map = new HashMap<>();
for(String word: s1.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}

for(String word: s2.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}

List<String> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
if(entry.getValue() == 1)
list.add(entry.getKey());
}

return list.toArray(new String[0]);
}
}
image_2022-01-05_02-07-38.png
45.5 KB
#N1694. Reformat Phone Number
problem link

#solution
class Solution {
public String reformatNumber(String number) {
StringBuilder sb = new StringBuilder();

for (char c : number.toCharArray()) {
if (Character.isDigit(c)) sb.append(c);
}

int i=0;
for(; i<sb.length()-4;){
sb.insert(i+3, '-');
i+=4;
}

if(sb.length() - i == 4)
sb.insert(i+2, '-');

return sb.toString();
}
}
image_2022-01-05_02-11-17.png
33.7 KB
#N1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
problem link

#solution
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
int ans=0;
for(String word: sentence.split(" ")){
if(word.startsWith(searchWord))
return ans+1;

ans++;
}

return -1;
}
}
image_2022-01-05_02-13-05.png
59.7 KB
#N1078. Occurrences After Bigram
problem link

#solution
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] arr=text.split(" ");
String[] ans = new String[100];
int count=0;
for(int i=0; i<arr.length-2; i++){
if(arr[i].equals(first)&&arr[i+1].equals(second)){
ans[i]=arr[i+2];
count ++;
}
}


String[] main=new String[count];

for(int i=0; i<count; i++){
for(int j=0; j<ans.length; j++){
if(ans[j]!=null){
main[i]=ans[j];
ans[j]=null;
break;
}
}
}

return main;
}
}
image_2022-01-05_18-01-47.png
21.2 KB
#N627. Swap Salary
problem link

#solution
update Salary
set sex = case
sex when 'm' then 'f'
else 'm'
end;
image_2022-01-05_18-17-32.png
26.5 KB
#N181. Employees Earning More Than Their Managers
problem link

#solution
select t1.name as Employee from Employee t1, Employee t2
where t1.managerId = t2.id and t1.salary>t2.salary;