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-10_00-49-07.png
39.1 KB
#N1331. Rank Transform of an Array
problem link

#solution
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] helper = Arrays.copyOf(arr, arr.length);
Arrays.sort(helper);
Map<Integer, Integer> rank = new HashMap<>();
int pos=1;
for(int n: helper)
if(rank.getOrDefault(n, 0) == 0) rank.put(n, rank.size()+1);

for(int i=0; i<arr.length; i++)
arr[i] = rank.get(arr[i]);

return arr;
}
}
image_2021-12-10_01-25-15.png
43.5 KB
#N1560. Most Visited Sector in a Circular Track
problem link

#solution
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
int l=rounds.length;
List<Integer> list = new ArrayList<>();

for(int i=rounds[0]; i<=rounds[l-1]; i++)
list.add(i);

if(list.size()>0) return list;

for(int i=1; i<=rounds[l-1]; i++)
list.add(i);
for(int i=rounds[0]; i<=n; i++)
list.add(i);

return list;
}
}
image_2021-12-10_01-26-42.png
30.4 KB
#N167. Two Sum II - Input Array Is Sorted
problem link

#solution
class Solution {
public int[] twoSum(int[] numbers, int target) {
int start = 0, end = numbers.length - 1;
while(start < end){
if(numbers[start] + numbers[end] == target) break;
if(numbers[start] + numbers[end] < target) start++;
else end--;
}
return new int[]{start + 1, end + 1};
}
}
image_2021-12-10_01-56-28.png
57.3 KB
#N1370. Increasing Decreasing String
problem link

#solution
class Solution {
public String sortString(String s) {
int count[] = new int[26];
StringBuilder sb=new StringBuilder();
for(char ch: s.toCharArray())
count[ch-'a']++;

while(sb.toString().length()<s.length()){
for(int i=0; i<26; i++){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}

for(int i=25; i>=0; i--){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}
}

return sb.toString();
}
}
image_2021-12-10_02-03-39.png
42.1 KB
#N1436. Destination City
problem link

#solution
class Solution {
public String destCity(List<List<String>> paths) {
Map<String, Integer> map = new HashMap<>();

for(List<String> list: paths){
map.put(list.get(0), map.getOrDefault(list.get(0), 0) -1);
map.put(list.get(1), map.getOrDefault(list.get(1), 0) +1);
}

for(Map.Entry<String, Integer> entry: map.entrySet())
if(entry.getValue()>0) return entry.getKey();

return new String();
}
}
image_2021-12-10_02-10-28.png
29.6 KB
#N1374. Generate a String With Characters That Have Odd Counts
problem link

#solution
class Solution {
public String generateTheString(int n) {
StringBuilder sb = new StringBuilder();

for(int i=0; i<n-1; i++)
sb.append('a');

return (n%2==0)?sb.append('b').toString():sb.append('a').toString();
}
}
#statistics
Till this time, 192 in total,
180 easy;
12 medium;
type of problem solutions have been posted
image_2021-12-12_23-31-05.png
37.4 KB
#N1941. Check if All Characters Have Equal Number of Occurrences
problem link

#solution
class Solution {
public boolean areOccurrencesEqual(String s) {
int freq[] = new int[26], count=0;
char temp=s.charAt(0);

for(char ch: s.toCharArray()){
if(ch == temp) count++;
freq[ch-'a']++;
}

for(int i: freq)
if(i != 0 && i != count) return false;

return true;
}
}
image_2021-12-12_23-43-22.png
33.4 KB
#N557. Reverse Words in a String III
problem link

#solution
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
String[] words = s.split(" ");

for(String word: words){
sb.append(" ");

sb.append(new StringBuilder(word).reverse());
}

return sb.toString().trim();
}
}
image_2021-12-12_23-53-03.png
34.8 KB
#N657. Robot Return to Origin
problem link

#solution
class Solution {
public boolean judgeCircle(String moves) {
int[] count = new int[4];

for(char ch: moves.toCharArray()){
if(ch == 'R') count[0]++;
else if(ch == 'L') count[1]++;
else if(ch == 'U') count[2]++;
else count[3]++;
}

return count[0] - count[1] == 0 && count[2] - count[3] == 0;
}
}
image_2021-12-12_23-58-55.png
38.3 KB
#N1768. Merge Strings Alternately
problem link

#solution
class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder();

for(int i = 0; i<Math.max(word1.length(), word2.length()); i++){
if(word1.length()>i)
sb.append(word1.charAt(i));

if(word2.length()>i)
sb.append(word2.charAt(i));

}

return sb.toString();
}
}
image_2021-12-13_00-32-47.png
23 KB
#N344. Reverse String
problem link

#solution
class Solution {
public void reverseString(char[] s) {
for(int i=0; i<s.length/2; i++){
char tmp = s[i];
s[i] = s[s.length-1-i];
s[s.length-1-i] = tmp;
}
}
}
image_2021-12-13_00-40-15.png
48.4 KB
#N1880. Check if Word Equals Summation of Two Words
problem link

#solution
class Solution {
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
int num1, num2, target;
num1 = func(firstWord);
num2 = func(secondWord);
target = func(targetWord);

return num1 + num2 == target;
}

public int func(String str){
int x=0;

for(char ch: str.toCharArray())
x = x*10 + (int)(ch-'a');

return x;
}
}
image_2021-12-13_00-50-22.png
46.2 KB
#N1935. Maximum Number of Words You Can Type
problem link

#solution
class Solution {
public int canBeTypedWords(String text, String brokenLetters) {
int broken[] = new int[26], count = 0;;

for(char ch: brokenLetters.toCharArray())
broken[ch-'a']++;

for(String word: text.split(" ")){
int countLength=0;

for(char ch: word.toCharArray())
if(broken[ch-'a'] == 0) countLength++;

if(countLength == word.length()) count++;
}

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