0% found this document useful (0 votes)
5 views

Leetcode important codes

Uploaded by

akankshyatarenia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Leetcode important codes

Uploaded by

akankshyatarenia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Leetcode q.

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

public static void main(String args[]){


Scanner ss=new Scanner(System.in);
String s=ss.nextLine();
String t=ss.nextLine();
isIsomorphic(s,t);
}
}

(OR)

import java.util.HashMap;

public class IsomorphicStrings {


public static boolean areIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}

HashMap<Character, Character> sToT = new HashMap<>();


HashMap<Character, Character> tToS = new HashMap<>();

for (int i = 0; i < s.length(); i++) {


char charS = s.charAt(i);
char charT = t.charAt(i);
// Check s to t mapping
if (sToT.containsKey(charS)) {
if (sToT.get(charS) != charT) {
return false; // Not isomorphic
}
} else {
sToT.put(charS, charT);
}

// 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;
}

public static void main(String[] args) {


// Example 1
String s1 = "egg";
String t1 = "add";
System.out.println(areIsomorphic(s1, t1)); // Output: 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
}
}

3. Reverse vowels of a string---

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.

A chocolate factory is packing chocolates into the packets. The chocolate


packets here represent an array of N number of integer values. The task
is to find the empty packets(0) of chocolate and push it to the end of the
conveyor belt(array).

Example 1 : N=8 and arr = [4,5,0,1,9,0,5,0]. There are 3 empty packets


in the given set. These 3 empty packets represented as O should be
pushed towards the end of the array

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

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

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 "";
}

StringBuilder sb = new StringBuilder();

for (int j = 0; j < strs[0].length(); j++) {


char c = strs[0].charAt(j);
for (int i = 1; i < strs.length; i++) {
if (j >= strs[i].length() || strs[i].charAt(j) != c) {
return sb.toString();
}
}
sb.append(c);
}

return sb.toString();
}
}

You might also like