Implement strStr()
public class Lc28 {
public static int strStr(String haystack, String needle) {
boolean haystackIsNUll = "".equals(haystack);
boolean needleIsNUll = "".equals(needle);
if (!haystackIsNUll && !needleIsNUll) {
} else if (!needleIsNUll) {
return -1;
} else {
return 0;
}
char chHaystack[] = haystack.toCharArray();
char chNeedle[] = needle.toCharArray();
int count = chNeedle.length;
int temp = 0;
for (int i = 0; i < chHaystack.length; i++) {
if (chHaystack[i] == chNeedle[0]) {
temp = i;
for (int j = 0; j < chNeedle.length && temp < chHaystack.length; j++, temp++) {
if (chHaystack[temp] == chNeedle[j]) {
count--;
} else {
break;
}
}
}
if (count == 0) {
return i;
} else {
count = chNeedle.length;
}
}
return -1;
}
public static int strStr2(String haystack, String needle) {
boolean haystackIsNUll = "".equals(haystack);
boolean needleIsNUll = "".equals(needle);
if (!haystackIsNUll && !needleIsNUll) {
} else if (!needleIsNUll) {
return -1;
} else {
return 0;
}
char chHaystack[] = haystack.toCharArray();
char chNeedle[] = needle.toCharArray();
int len = chHaystack.length - chNeedle.length;
for (int i = 0; i <= len; i++) {
if (haystack.substring(i, chNeedle.length + i).equals(needle)) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
String haystack = "bbaa";
String needle = "aab";
System.out.println(strStr2(haystack, needle));
}
}
Longest Common Prefix
public class Lc14 {
public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
String prefix = strs[0];
for (int i = 0; i < strs.length; i++) {
if (prefix.length() > strs[i].length()) {
prefix = strs[i];
}
}
for (int i = 0; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
return "";
}
}
}
return prefix;
}
public static void main(String[] args) {
String[] strs = { "flower", "flow", "flight" };
System.out.println(longestCommonPrefix(strs));
}
}
Length of Last Word
public class Lc58 {
public static int lengthOfLastWord(String s) {
if (s.length() == 0 || " ".equals(s)) {
return 0;
}
char[] chs = s.trim().toCharArray();
int len = 0;
for (int i = 0; i < chs.length; i++) {
if ((chs[i] >= 'a' && chs[i] <= 'z') || (chs[i] >= 'A' && chs[i] <= 'Z')) {
len++;
} else if (chs[i] == ' '&&i==chs.length-1) {
if (len > 0) {
return len;
}
}else if(chs[i] == ' '){
len = 0;
}
}
return len;
}
public static int lengthOfLastWord1(String s) {
return s.trim().length() - s.trim().lastIndexOf(" ") - 1;
}
public static void main(String[] args) {
String str = "Today is a nice day";
System.out.println(lengthOfLastWord1(str));
}
}
First Unique Character in a String
import java.util.LinkedHashMap;
import java.util.Map;
public class Lc387 {
public static int firstUniqChar(String s) {
if ("".equals(s)) {
return -1;
}
char[] chs = s.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (int i = 0; i < chs.length; i++) {
Character key = chs[i];
if (map.containsKey(key)) {
Integer count = map.get(key);
map.put(key, ++count);
} else {
map.put(key, 1);
}
}
Character firstC = ' ';
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
firstC = entry.getKey();
break;
}
}
for (int i = 0; i < chs.length; i++) {
if (chs[i] == firstC) {
return i;
}
}
return -1;
}
public static int firstUniqChar1(String s) {
if ("".equals(s)) {
return -1;
}
char[] chs = s.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (int i = 0; i < chs.length; i++) {
Character key = chs[i];
if (map.containsKey(key)) {
Integer count = map.get(key);
map.put(key, ++count);
} else {
map.put(key, 1);
}
}
Character firstC = ' ';
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
firstC = entry.getKey();
break;
}
}
return s.indexOf(firstC);
}
public static void main(String[] args) {
String s = "loveleetcode";
System.out.println(firstUniqChar(s));
}
}
Ransom Note
import java.util.HashMap;
import java.util.Map;
public class Lc383 {
public static boolean canConstruct(String ransomNote, String magazine) {
if (!"".equals(ransomNote) && "".equals(magazine)) {
return false;
}
char[] chs = magazine.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char key : chs) {
if (map.containsKey(key)) {
Integer count = map.get(key);
map.put(key, ++count);
} else {
map.put(key, 1);
}
}
char[] chsRansonNote = ransomNote.toCharArray();
for (char c : chsRansonNote) {
int count = 0;
count = map.get(c) != null ? map.get(c) : 0;
count--;
map.put(c, count);
if (count < 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String ransomNote = "";
String magazine = "a";
System.out.println(canConstruct(ransomNote, magazine));
}
}
Reverse String
public class Lc344 {
public static void reverseString(char[] s) {
for (int i = 0; i < Math.round(s.length / 2); i++) {
char temp = s[i];
s[i] = s[s.length - 1 - i];
s[s.length - 1 - i] = temp;
}
}
public static void main(String[] args) {
char[] s = { 'h', 'e', 'l', 'l', 'o' };
reverseString(s);
}
}