Skip to content

Instantly share code, notes, and snippets.

@mding5692
Created September 20, 2016 03:44
Show Gist options
  • Save mding5692/6c8a9bb138a92a09203547e0dadfad33 to your computer and use it in GitHub Desktop.
Save mding5692/6c8a9bb138a92a09203547e0dadfad33 to your computer and use it in GitHub Desktop.
2016-2017 Western Tech Interview Prep Session #1 Questions
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0 ) {
return 0;
}
if (s.length() == 1) {
return 1;
}
int count = 0;
int i = 0;
HashMap<Character,Integer> countMap = new HashMap<Character,Integer>();
ArrayList<Integer> countList = new ArrayList<Integer>();
while ( i < s.length() ) {
for (int j = i; j < s.length(); j++) {
if (countMap.containsKey(s.charAt(j)) == false) {
count++;
countMap.put(s.charAt(j), count);
} else {
countMap.clear();
countList.add(count);
count = 0;
break;
}
}
i++;
}
return countLargest(countList);
}
public int countLargest(ArrayList<Integer> list) {
int count = 0;
for (int n = 0; n < list.size(); n++) {
int arrNum = list.get(n);
if (arrNum > count) {
count = arrNum;
}
}
return count;
}
}
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (n != 0 || m != 0) {
for (int i = 0; i < n; i++) {
nums1[m+i] = nums2[i];
}
Arrays.sort(nums1);
}
}
}
public class Solution {
public boolean isPalindrome(String s) {
if (s.length() == 0) {
return true;
}
s = s.replaceAll(" ","");
s = s.replaceAll("[^A-Za-z0-9 ]", "");
s = s.toLowerCase();
for (int i = 0; i < s.length()/2; i++) {
int end = s.length()-1 - i;
if (s.charAt(i) != s.charAt(end)) {
System.out.println(s.charAt(i));
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment