Leetcode important codes
Leetcode important codes
28 Given two strings needle and haystack, return the index of the first
occurrence of needlein haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.
Example 2:
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.
class Solution {
public static int strStr(String haystack, String needle) {
int nee_len=needle.length();
int hay_len=haystack.length();
String sub;
for(int i=0;i<=hay_len-nee_len;i++){
sub = haystack.substring(i,i+nee_len);
if(needle.equals(sub)){
return i;
}
}
return -1;
}
public static void main(String args[]){
Scanner ss=new Scanner(System.in);
String hay=ss.nextLine();
String nee=ss.nextLine();
strStr(hay,nee);
}
}
2. Isomorphic strings--
class Solution {
public static boolean isIsomorphic(String s, String t) {
if(t.length()!=s.length()){
return false;
}
Map<Character,Character> map=new HashMap<>();
for(int i=0;i<s.length();i++){
char str1=s.charAt(i),str2=t.charAt(i);
if(map.containsKey(str1)){
if(map.get(str1)!=str2){
return false;
}
}
else {
if(map.containsValue(str2)){
return false;
}
map.put(str1,str2);
}
}
return true;
}
(OR)
import java.util.HashMap;
// Check t to s mapping
if (tToS.containsKey(charT)) {
if (tToS.get(charT) != charS) {
return false; // Not isomorphic
}
} else {
tToS.put(charT, charS);
}
}
return true;
}
// Example 2
String s2 = "foo";
String t2 = "bar";
System.out.println(areIsomorphic(s2, t2)); // Output: false
// Example 3
String s3 = "paper";
String t3 = "title";
System.out.println(areIsomorphic(s3, t3)); // Output: true
}
}
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
class Solution {
public String reverseVowels(String s) {
char[] c=s.toCharArray();
int start=0;
int end=s.length()-1;
while(start<end){
while(start<end && !isVowel(c[start])){
start++;
}
while(start<end && !isVowel(c[end])){
end--;
}
if(start<end){
char temp=c[start];
c[start]=c[end];
c[end]=temp;
start++;
end--;
}
}
return new String(c);
}
public static boolean isVowel(char c){
return "aeiouAEIOU".indexOf(c) != -1;
}
}
4.
Input : 8 – Value of N
[4,5,0,1,9,0,5,0]
Output: 4 5 1 9 5 0 0 0
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i< n;i++)
arr[i]=sc.nextInt();
int count=0;
for(int i=0;i< n;i++)
if(arr[i]!=0)
arr[count++]=arr[i];
for(int i=count;i < n;i++)
arr[i]=0;
for(int i=0;i< n;i++)
System.out.print(arr[i]+" ");
}
}
5. Palindrome
class Solution {
public boolean isPalindrome(int x) {
int old=x,s,rev=0;
while(x>0){
s=x%10;
rev=(rev*10)+s;
x=x/10;
}
return (rev==old);
}
}
6. Roman to Integer
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is
written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX +
V + II.
///////SWITCH STATEMENTS///////
class Solution {
public int romanToInt(String s) {
int prev=0,ans=0,num=0;
for(int i=s.length()-1;i>=0;i--){
switch(s.charAt(i)){
case 'I'-> num=1;
case 'V'-> num=5;
case 'X'-> num=10;
case 'L'-> num=50;
case 'C'-> num=100;
case 'D'-> num=500;
case 'M'-> num=1000;
}
if(num < prev){
ans -= num;
}
else{
ans += num;
}
prev=num;
}
return ans;
}
}
7. Write a function to find the longest common prefix string amongst an array of
strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
//////////
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
return sb.toString();
}
}