-1

I am not able to understand what's the difference between the two codes mentioned below. In the second code, I have done changes in line 1 and line 2 only.

  • You are given two strings s and t.
  • String t is generated by random shuffling string s and then adding one more letter at a random position.
  • Return the letter that was added to t.
var findTheDifference = function(s, t) {

    s.split("").sort(); //line 1
    t.split("").sort(); //line 2
    
    for(let i=0; i<t.length; i++){
        if(t[i] != s[i])
             return t[i];
    }   
};

var findTheDifference = function(s, t) {

    s = s.split("").sort(); //line 1
    t = t.split("").sort(); //line 2
    
    for(let i=0; i<t.length; i++){
        if(t[i] != s[i])
             return t[i];
    }  
};

Sample input:

"ymbgaraibkfmvocpizdydugvalagaivdbfsfbepeyccqfepzvtpyxtbadkhmwmoswrcxnargtlswqemafandgkmydtimuzvjwxvlfwlhvkrgcsithaqlcvrihrwqkpjdhgfgreqoxzfvhjzojhghfwbvpfzectwwhexthbsndovxejsntmjihchaotbgcysfdaojkjldprwyrnischrgmtvjcorypvopfmegizfkvudubnejzfqffvgdoxohuinkyygbdzmshvyqyhsozwvlhevfepdvafgkqpkmcsikfyxczcovrmwqxxbnhfzcjjcpgzjjfateajnnvlbwhyppdleahgaypxidkpwmfqwqyofwdqgxhjaxvyrzupfwesmxbjszolgwqvfiozofncbohduqgiswuiyddmwlwubetyaummenkdfptjczxemryuotrrymrfdxtrebpbjtpnuhsbnovhectpjhfhahbqrfbyxggobsweefcwxpqsspyssrmdhuelkkvyjxswjwofngpwfxvknkjviiavorwyfzlnktmfwxkvwkrwdcxjfzikdyswsuxegmhtnxjraqrdchaauazfhtklxsksbhwgjphgbasfnlwqwukprgvihntsyymdrfovaszjywuqygpvjtvlsvvqbvzsmgweiayhlubnbsitvfxawhfmfiatxvqrcwjshvovxknnxnyyfexqycrlyksderlqarqhkxyaqwlwoqcribumrqjtelhwdvaiysgjlvksrfvjlcaiwrirtkkxbwgicyhvakxgdjwnwmubkiazdjkfmotglclqndqjxethoutvjchjbkoasnnfbgrnycucfpeovruguzumgmgddqwjgdvaujhyqsqtoexmnfuluaqbxoofvotvfoiexbnprrxptchmlctzgqtkivsilwgwgvpidpvasurraqfkcmxhdapjrlrnkbklwkrvoaziznlpor"

"qhxepbshlrhoecdaodgpousbzfcqjxulatciapuftffahhlmxbufgjuxstfjvljybfxnenlacmjqoymvamphpxnolwijwcecgwbcjhgdybfffwoygikvoecdggplfohemfypxfsvdrseyhmvkoovxhdvoavsqqbrsqrkqhbtmgwaurgisloqjixfwfvwtszcxwktkwesaxsmhsvlitegrlzkvfqoiiwxbzskzoewbkxtphapavbyvhzvgrrfriddnsrftfowhdanvhjvurhljmpxvpddxmzfgwwpkjrfgqptrmumoemhfpojnxzwlrxkcafvbhlwrapubhveattfifsmiounhqusvhywnxhwrgamgnesxmzliyzisqrwvkiyderyotxhwspqrrkeczjysfujvovsfcfouykcqyjoobfdgnlswfzjmyucaxuaslzwfnetekymrwbvponiaojdqnbmboldvvitamntwnyaeppjaohwkrisrlrgwcjqqgxeqerjrbapfzurcwxhcwzugcgnirkkrxdthtbmdqgvqxilllrsbwjhwqszrjtzyetwubdrlyakzxcveufvhqugyawvkivwonvmrgnchkzdysngqdibhkyboyftxcvvjoggecjsajbuqkjjxfvynrjsnvtfvgpgveycxidhhfauvjovmnbqgoxsafknluyimkczykwdgvqwlvvgdmufxdypwnajkncoynqticfetcdafvtqszuwfmrdggifokwmkgzuxnhncmnsstffqpqbplypapctctfhqpihavligbrutxmmygiyaklqtakdidvnvrjfteazeqmbgklrgrorudayokxptswwkcircwuhcavhdparjfkjypkyxhbgwxbkvpvrtzjaetahmxevmkhdfyidhrdeejapfbafwmdqjqszwnwzgclitdhlnkaiyldwkwwzvhyorgbysyjbxsspnjdewjxbhpsvj"


Output : "t"
rakeshboliya
  • 103
  • 12
Raaz
  • 9
  • 1
    When you do `s.split("").sort()`, it does not change `s`. `s` stays the same. However, when you do `s = s.split("").sort()`, you are assigning `s` to be this new split, sorted array. It changes. That is the difference. – cSharp Aug 08 '22 at 05:49
  • in the first code you discard the result of the split+sort – Jaromanda X Aug 08 '22 at 05:50
  • Just a note: you have an outbound access in your code, if the added character is in the last position of t, then you do s[t.length-1] which isn’t valid – e.ad Aug 08 '22 at 05:54

1 Answers1

0

Strings are immutable meaning you cannot change them. If you want to change them then you need to assign them to a new variable.

In this example we take the values of s and t and split them than sort them and assign them to s and t to do something with them

const s = 'psakdjhflksadjhfalskdjhflaskjhdf';
const t = 'lsdfl;sakdjhflsdkjfl';

const findTheDifference = function (s, t) {
  s = s.split('').sort(); //line 1
  t = t.split('').sort(); //line 2
  console.log(s);
  console.log(t);

  for (let i = 0; i < t.length; i++) {
    if (t[i] != s[i]) return t[i];
  }
};

Here we take the s and t, split and sort them but then use the original strings to do logic with

    const s = 'psakdjhflksadjhfalskdjhflaskjhdf';
    const t = 'lsdfl;sakdjhflsdkjfl';

    const findTheDifference = function (s, t) {
      s.split('').sort(); //line 1
      t.split('').sort(); //line 2
      console.log(s);
      console.log(t);

      for (let i = 0; i < t.length; i++) {
        if (t[i] != s[i]) return t[i];
      }
    };```
Deyan Petrov
  • 150
  • 4