当前位置:网站首页>String Day6 of Li Kou daily practice

String Day6 of Li Kou daily practice

2022-06-23 19:37:00 InfoQ

Force buckle the string of one practice per day Day6

In front of the word

Hello everyone ! This article will introduce 2 Zhou solved the problem of data structure , This paper will take three questions as the background , Introduce the classic Sudoku and sorting algorithm , The display language is java( The blogger's learning language is java). What about today , It's the fifth day that bloggers start to brush force buckle , If you want to start preparing for your algorithm interview , You can follow my footsteps , Common progress . We are all partners fighting side by side , Work hard together , What a long long road! , I will go up and down , I believe we can all get what we expect offer, To rush !

Blog home page : The blog home page of Beijing and JIUPU
Welcome to pay attention to the likes and comments
This article is original by Beijing and JIUPU ,csdn First episode !
Series column :java Study
Starting time :2022 year 5 month 10 Japan
You do march and April , There will be an answer in August and September , Come on
Refer to the online programming website : Power button
If you think the blogger's article is good , Please support the blogger for the third company
Last words , The author is a newcomer , Not doing well in many ways , Welcome the boss to correct , Study together , To rush

Navigation assistant

[TOC]



picture


LeetCode 387. The first unique character in the string

Given a string  s , find   Its first non repeating character , And return its index  . If it doesn't exist , Then return to  -1 .

Example  1:

Input : s = "leetcode"
Output : 0
Example  2:

Input : s = "loveleetcode"
Output : 2
Example  3:

Input : s = "aabb"
Output : -1

Their thinking

Traverse the string. If the first index and the last index of the current character are not the same, continue to traverse , It's the same return .

Source code

class Solution {
 public int firstUniqChar(String s) {
 for (int i = 0; i < s.length(); i++) {
 if (s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) {
 return i;
 }
 }
 return -1;
 }
}

🦪LeetCode383. Ransom letter

Here are two strings :ransomNote  and  magazine , Judge  ransomNote  Can it be done by  magazine  The characters inside make up .

If possible , return  true ; Otherwise return to  false .

magazine  Each character in can only be in  ransomNote  Used once in .

Example  1:

Input :ransomNote = &quot;a&quot;, magazine = &quot;b&quot;
Output :false
Example  2:

Input :ransomNote = &quot;aa&quot;, magazine = &quot;ab&quot;
Output :false
Example  3:

Input :ransomNote = &quot;aa&quot;, magazine = &quot;aab&quot;
Output :true

Their thinking

Today's question means : Check  ransomNote  Are all the characters in  magazine  in , And  magazine  Each character in can only be used once , that , We can count it directly first  magazine  Frequency of all characters in , Re traversal  ransomNote, Every time you traverse a character, subtract the number of this character from the word frequency table , Until it's not enough , You can return  false, otherwise , return  true.

Source code

class Solution {
 public boolean canConstruct(String ransomNote, String magazine) {
 int [] count=new int[26];
 for(char m:magazine.toCharArray()){
 count[m-'a']++;
 }
 for(char r:ransomNote.toCharArray()){
 if(count[r-'a']<=0)return false;
 count[r-'a']--;
 }
 return true;
 }
}

Leetcode242. Effective alphabetic words

Given two strings  s  and  t , Write a function to determine  t  Whether it is  s  Letter heteronym of .

Be careful : if  s  and  t  Each character in the has the same number of occurrences , said  s  and  t  They are mutually alphabetic words .

Example  1:

Input : s = &quot;anagram&quot;, t = &quot;nagaram&quot;
Output : true
Example  2:

Input : s = &quot;rat&quot;, t = &quot;car&quot;
Output : false

🥫 Their thinking

Sort tt  yes  ss  The heterotopic words of are equivalent to 「 Two strings are sorted equal 」. So we can do string  ss  and  tt  Sort them separately , You can judge whether the sorted strings are equal . Besides , If  ss  and  tt  The length is different ,tt  It must not be  ss  The heterotopic words of .

Source code

class Solution {
 public boolean isAnagram(String s, String t) {
 if(s.length()!=t.length()){
 return false;
 }
 char[] str1=s.toCharArray();
 char[] str2=t.toCharArray();
 Arrays.sort(str1);
 Arrays.sort(str2);
 return Arrays.equals(str1,str2);
 }
}

summary

Pass these three questions , We learned for Enhanced cycle , Reviewed the knowledge of arrays and loops , So , Look forward to the next article , Make progress with me , Try harder every day , Take a bigger step


I think the article is well written , Like comments, pay attention to a wave , Love you

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231832547743.html